Skip to content

Commit 6b23e03

Browse files
authored
Create longestConsecutiveSubseq.java
1 parent 6d16bae commit 6b23e03

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

longestConsecutiveSubseq.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int longestConsecutive(int[] nums) {
3+
if(nums.length==0)
4+
return 0;
5+
if(nums.length==1)
6+
return 1;
7+
Arrays.sort(nums);
8+
int count = 1;
9+
int maxCount =1;
10+
for(int i=1;i<nums.length;i++){
11+
if((nums[i-1]==nums[i])){
12+
continue;
13+
}
14+
else if(nums[i-1]+1==nums[i]){
15+
count++;
16+
//System.out.println(arr[i-1]);
17+
}else{
18+
count = 1;
19+
}
20+
21+
if(count>maxCount)
22+
maxCount = count;
23+
}
24+
return maxCount;
25+
26+
}
27+
}

0 commit comments

Comments
 (0)