Java Solution using Collection.sort


#1

public class Solution {
// DO NOT MODIFY THE LIST. IT IS READ ONLY
public int longestConsecutive(final List A) {
ArrayList B = new ArrayList<>(A);
Collections.sort(B);
int glob =0;
boolean flage =false;
for(int i=0;i<B.size()-1;i++){
int temp =1;
while(i+1<B.size()&&Math.abs(B.get(i+1)-B.get(i))<=1){
if(B.get(i+1)!=B.get(i)&&B.get(i+1)-B.get(i)==1){
temp++;
}
i++;
flage = true;
}
if(temp>glob){
glob = temp;
}
}
if(glob<1)
return 1;
return glob;
}
}


#2

This is not running in O(N) because the sort statement will in the best case add a complexity O(N log N). So in the end this solution is running in O(N + NlogN) ~ O(NlogN).


#3

Yeah i know that but it’s accepting the solution :sweat_smile::sweat_smile: