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. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int[] arr = {0,3,7,2,5,8,4,6,0,1};
  14. System.out.println("longest consequtive sequence is "+ lcs(arr));
  15. }
  16. static int lcs(int[] arr){
  17. HashSet<Integer> set = new HashSet<>();
  18. for(int i=0;i<arr.length;i++){
  19. set.add(arr[i]);
  20. }
  21. int max = 1;
  22. for(int i=0;i<arr.length;i++){
  23. if(!set.contains(arr[i]-1)){
  24. int length = 1;
  25. while(set.contains(arr[i]+length)){
  26. length++;
  27. }
  28. max = Math.max(max,length);
  29. }
  30. }
  31. return max;
  32. }
  33.  
  34. }
Success #stdin #stdout 0.12s 53464KB
stdin
Standard input is empty
stdout
longest consequtive sequence is 9