-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestsequence.java
More file actions
45 lines (40 loc) · 873 Bytes
/
Copy pathlongestsequence.java
File metadata and controls
45 lines (40 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Arrays;
import java.util.Scanner;
public class longestsequence {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int []arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int i=0,mx=0;
int l=0;
int fi=0;
int cnt=1;
while(i<n-1)
{
if(arr[i+1]==arr[i]+1)
{
cnt++;
}
else{
cnt=0;
l=i;
}
if(cnt>mx)
{
mx=cnt;
fi=l+mx;
}
i++;
}
for(int j=fi-mx;j<fi;j++)
{
System.out.print(arr[j]+" ");
}
}
}