-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem7.java
39 lines (28 loc) · 1.02 KB
/
problem7.java
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
package sheetArrays;
import java.util.Arrays;
import java.util.Scanner;
public class problem7 {
public static void main(String[] args) {
System.out.println("Enter size of integer array : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter value : ");
arr[i]=in.nextInt();
}
Arrays.sort(arr);
int length=1,maxLength=length;
for (int i = 1; i < n; i++) {
if(arr[i]==arr[i-1]+1 ){
length++;
}
else{
if(length>maxLength)maxLength=length;
length=1;
}
}
if(length>maxLength)maxLength=length;
System.out.println("The length of longest consecutive elements sequence : "+maxLength);
} //[49, 1, 3, 200, 2, 4, 70, 5]
}