ElonReveMusk / Hactoberfest-2022

Contribute to all your PR's over here to get some awesome Hactoberfest goodies.
Apache License 2.0
7 stars 76 forks source link

Longest Consecutive Subsequence #73

Open m-soumik opened 1 year ago

m-soumik commented 1 year ago

The idea is to first sort the array and find the longest subarray with consecutive elements. After sorting the array and removing the multiple occurrences of elements, run a loop and keep a count and max (both initially zero). Run a loop from start to end and if the current element is not equal to the previous (element+1) then set the count to 1 else increase the count. Update max with a maximum of count and max.