fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone{
  9. public static void main (String[] args) throws java.lang.Exception{
  10. Integer[] A = {1, 2, 3, 6, 7, 8, 9, 20, 22};
  11.  
  12. System.out.print("Numbers ");
  13. int start = 0, end;
  14. while((end = start) < A.length){
  15. while(end + 1 < A.length && A[end + 1] == A[end] + 1) end++;
  16. System.out.print(A[start] +
  17. (end == start ? "":(" through " + A[end]))+
  18. (end < A.length - 1 ? ", ":""));
  19. start = end + 1;
  20. }
  21. }
  22. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
Numbers 1 through 3, 6 through 9, 20, 22