-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlt47.cpp
More file actions
29 lines (28 loc) · 750 Bytes
/
lt47.cpp
File metadata and controls
29 lines (28 loc) · 750 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
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int n=nums.size();
sort(nums.begin(),nums.end());
int difference=0;
int count=1;
int maxcounttrack=0;
if(nums.size()==1){
return 1;
}
for(int i=1;i<n;i++){
if(nums[i]-1==nums[i-1]){
count++;
maxcounttrack=max(maxcounttrack,count);
}
else if(nums[i]==nums[i-1]){
count=count;
maxcounttrack=max(maxcounttrack,count);
}
else{
count=1;
maxcounttrack=max(maxcounttrack,count);
}
}
return maxcounttrack;
}
};