-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4_array_longestConsecutiveSequence.cpp
More file actions
38 lines (34 loc) · 1.11 KB
/
day4_array_longestConsecutiveSequence.cpp
File metadata and controls
38 lines (34 loc) · 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
int longestConsecutive(vector<int>& arr) {
int n = arr.size();
if (n== 0) return 0;
sort(arr.begin(), arr.end());
// for(int i= 0; i<n;i++) cout << arr[i] << " ";
int count = 1, maxSum = 1;
// cout << n << endl;
for(int i = 0; i< n;i++){
if (i+1 == n) break;
int d = (arr[i+1] - arr[i]) ;
if(d == 1 or d==0){
if (d == 1 ) {
count++;
maxSum = max(count, maxSum);
}
}
else count = 1 ;
// cout << "the count after "<< i << "th round is " << count << endl;
}
return maxSum;
}
int main(){
int t;cin>>t;while(t--){
int a, n;cin>>n;
vector<int> v(n);
for(int i= 0; i<n;i++) cin>>v[i];
// sort(arr.begin(), arr.end());
// for(int i= 0; i<n;i++) cout << arr[i] << " ";
cout << longestConsecutive(v)<<endl;
}
return 0;
}