Skip to content

Update 06 longest_band.cpp #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 29 additions & 32 deletions 01 Arrays & Vectors/06 longest_band.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,35 @@
using namespace std;

int largestBand(vector<int> arr){
int n = arr.size();
unordered_set<int> s;
int n = arr.size();
unordered_set<int> s;

//Data inside a set
for(int x : arr){
s.insert(x);
}

//Iterate over the arr
int largestLen = 1;

for(auto element : s){
int parent = element - 1;

if(s.find(parent)==s.end()){
//find entire band / chain starting from element
int next_no = element + 1;
int cnt = 1;

while(s.find(next_no)!=s.end()){
next_no++;
cnt++;
}

if(cnt>largestLen){
largestLen = cnt;
}
}
}


return largestLen;
//Data inside a set
for(int x : arr){
s.insert(x);
}

//Iterate over the arr
int largestLen = 1;
for(auto element : s){
int parent = element - 1;
if(s.find(parent)==s.end()){

//find entire band / chain starting from element
int next_no = element + 1;
int cnt = 1;

while(s.find(next_no)!=s.end()){
next_no++;
cnt++;
}
if(cnt>largestLen){
largestLen = cnt;
}
}
}
return n>0 ? largestLen:0;
}
}


Expand All @@ -46,4 +43,4 @@ int main(){
cout << largestBand(arr)<<endl;

return 0;
}
}