-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCon.java
More file actions
31 lines (30 loc) · 863 Bytes
/
LongestCon.java
File metadata and controls
31 lines (30 loc) · 863 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
import java.util.*;
public class LongestCon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
Stack<Integer> st = new Stack<>();
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
for(int num : arr){
st.push(num);
}
int currLen = 0;
int maxLen = Integer.MIN_VALUE;
while(!(st.isEmpty())){
int top = st.pop();
int nextTop = st.peek();
if(top - nextTop != 1){
maxLen = Math.max(maxLen, currLen);
currLen = 0;
}else{
currLen++;
}
}
maxLen = Math.max(maxLen, currLen);
System.out.println(maxLen);
}
}