diff --git a/01 Arrays & Vectors/06 longest_band.cpp b/01 Arrays & Vectors/06 longest_band.cpp index a074527..929bad5 100644 --- a/01 Arrays & Vectors/06 longest_band.cpp +++ b/01 Arrays & Vectors/06 longest_band.cpp @@ -4,38 +4,35 @@ using namespace std; int largestBand(vector arr){ - int n = arr.size(); - unordered_set s; + int n = arr.size(); + unordered_set 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; + } } @@ -46,4 +43,4 @@ int main(){ cout << largestBand(arr)<