Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d70e1c

Browse files
Create Longest Consecutive Sequence
1 parent dac850d commit 0d70e1c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎Hashmaps/Longest Consecutive Sequence

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<unordered_map>
2+
vector<int> longestConsecutiveIncreasingSequence(int *arr, int n) {
3+
unordered_map<int,int> ourmap;
4+
bool isStarting=false;
5+
for(int i=0;i<n;i++){
6+
ourmap[arr[i]]=1;
7+
}
8+
int overallmax=0;
9+
int overallstart;
10+
int overallend;
11+
int start;
12+
int prev;
13+
for(int i=0;i<n;i++){
14+
if(ourmap[arr[i]]==1){
15+
start=arr[i];
16+
int maxlength=1;
17+
int temp=start+1;
18+
while(ourmap[temp]>0){
19+
maxlength+=1;
20+
ourmap[temp]=0;
21+
temp++;
22+
}
23+
temp=start-1;
24+
if(ourmap[temp]!=1){
25+
prev=start;
26+
}
27+
else{
28+
while(ourmap[temp]==1){
29+
maxlength+=1;
30+
ourmap[temp]=0;
31+
prev=temp;
32+
temp--;
33+
}
34+
}
35+
if(maxlength>overallmax||(maxlength==overallmax&&!isStarting)){
36+
if(maxlength>overallmax){
37+
isStarting=false;
38+
}
39+
overallmax=maxlength;
40+
overallstart=prev;
41+
overallend=overallstart+overallmax-1;
42+
if(arr[i]==overallstart){
43+
isStarting=true;
44+
}
45+
}
46+
}
47+
}
48+
vector<int> ans;
49+
ans.push_back(overallstart);
50+
ans.push_back(overallend);
51+
return ans;
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /