Skip to content
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;
}
}