Longest Consecutive Subsequence in C++



Given an array of integers, determine the length of the longest subsequence where the elements are consecutive integers, regardless of their order within the subsequence.

Input 

arr = {100, 4, 200, 1, 3, 2}

Output 

Length of the longest consecutive sequence is: 4

Different approaches for longest consecutive subsequence

The following are the approaches to get longest consecutive subsequence

Approach 1: By Sorting the Array

Below are the steps to get the longest consecutive subsequence in C++ using Array
  • Sort the given array using an inbuilt sort function.
  • Maintain two variables, ans and cnt. Initialize cnt to 1.
  • Run a loop from i = 1 to the size of the array. Whenever arr[i] = arr[i-1] + 1, increment cnt by 1; otherwise, reset cnt to 1. If arr[i] is equal to previous element do not modify cnt variable.
  • Update the ans variable with the maximum of the current cnt and ans.

Example

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int longestConsecutiveSequence(vector<int>& arr) {
    sort(arr.begin(), arr.end());
    if (arr.empty()) return 0;

    int ans = 1;
    int cnt = 1;
    for (int i = 1; i < arr.size(); i++) {
        if (arr[i] == arr[i - 1] + 1) {
            cnt++;
        } else if (arr[i] != arr[i - 1]) {
            cnt = 1;
        }
        ans = max(ans, cnt);
    }

    return ans;
}

int main() {
    vector<int> arr = {100, 4, 200, 1, 3, 2};
    cout << "Length of the longest consecutive sequence is: " << longestConsecutiveSequence(arr) << endl;
    return 0;
}

Output

Length of the longest consecutive sequence is: 4
Time Complexity: O(N log N) since sorting the array takes O(N log N) time.
Space Complexity: O(1), assuming the sorting algorithm is done in-place.

Approach 2: By Using Set

Below are the steps to get the longest consecutive subsequence in C++ using Set
  • Insert all the elements into a set.
  • Traverse the array from i = 0 to i = n.
  • At each iteration, check whether arr[i - 1] exists in the set. If it does, continue to the next iteration; otherwise, move to Step 4.
  •  Initialize a variable cnt and set it to 1. While the set contains the next number, increment cnt by 1.
  • Update the ans variable with the maximum of the current cnt and ans.

Example

#include <iostream>
#include <set>
#include <unordered_set>
#include <vector>

using namespace std;

int longestConsecutiveSequence(vector<int>& arr) {
    unordered_set<int> elements(arr.begin(), arr.end());
    int ans = 0; 
    for (int i = 0; i < arr.size(); i++) {
        if (elements.find(arr[i] - 1) == elements.end()) {
            int cnt = 1;
            int current = arr[i];
            while (elements.find(current + 1) != elements.end()) {
                cnt++;
                current++;
            }
            ans = max(ans, cnt);
        }
    }

    return ans;
}

int main() {
    vector<int> arr = {100, 4, 200, 1, 3, 2};
    cout << "Length of the longest consecutive sequence is: " << longestConsecutiveSequence(arr) << endl;
    return 0;
}

Output

Length of the longest consecutive sequence is: 4

Time Complexity: O(N) since inserting into a set takes O(1) time, and this operation is performed N times.

Space Complexity: O(N) since a set is used to store N elements.

Updated on: 2024-09-02T18:02:19+05:30

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements