For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4].
public class Array {
public static void main(String args[]){
int a[]={10,15,1,2,3,4,5,11,12};
int b=1;
int c=0;
for(int i=0;i<a.length-1;i++){
if(a[i]-a[i+1]==-1){
b=b+1;
c=c+1;
if(b>=c)
{
System.out.println(a[i]);
}
else{
b=0;
}
}
}
}
}
But i am getting output as 1 2 3 4 11 whereas the output should be 1 2 3 4 5.
How to get the required output,whats the mistake in a code?
Array.sort(a);or you can create a for loop using bubble algorithm