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. int[] array = {1,8,3,2,4,5,9,7,8,1,0,4,5,6};
  11. StringBuilder sb = new StringBuilder("" + array[0]);
  12. String sequence = "";
  13. int count = 1;
  14. int maxCount = 1;
  15.  
  16. for (int i=1; i < array.length; i++) {
  17. if (array[i] == array[i-1] + 1) {
  18. count++;
  19. sb.append(",").append(array[i]);
  20. }
  21. if (array[i] != array[i-1] + 1 || i == array.length - 1) {
  22. if (count > maxCount) {
  23. maxCount = count;
  24. sequence = sb.toString();
  25. }
  26. count = 1;
  27. sb = new StringBuilder("" + array[i]);
  28. }
  29. }
  30.  
  31. System.out.println("The longest sequence was " + sequence + ", with a length of: " + maxCount);
  32. }
  33. }
Success #stdin #stdout 0.12s 36696KB
stdin
Standard input is empty
stdout
The longest sequence was 4,5,6, with a length of: 3