Find Longest Increasing Subsequence in C++



A subsequence is a sequence that can be derived from another sequence by deleting some elements and without changing the order of elements in sequence. For example, the sequences [3, 10], [3, 2, 20] and [3, 10, 20] are some of the subsequences of [3, 10, 2, 1, 20].

Longest Increasing Subsequence(LIS) is the longest of all the subsequences that are having elements in increasing order. In this article, we will learn how to write a C++ program to find the length of longest increasing subsequence for a given sequence. In other words, we are provided with a sequence of integers, and we need write a c++ program to find the length of longest increasing subsequence of the given sequence. For example,

// Example 1
Input: [10, 22, 9, 33, 21, 50, 41, 60]
Output: 5
Explanation: The LIS is [10, 22, 33, 50, 60]

// Example 2
Input: [10, 22, 9, 33, 21, 50, 41, 60]
Output: 4
Explanation: The LIS is [-1, 0, 2, 3]

Find Length of Longest Subsequence

Here is the list of all the approaches to find length of longest subsequence using c++ program, which we will be discussing in this article with stepwise explanation and complete example codes.

Using Dynamic Programming to Find Length of Longest Subsequence

The idea behind this approach is to divide the problem into sub problems. We can see that there are many subproblems, inside this implementation that are solved again and again. So this problem has Overlapping Substructure property and recomputation of same subproblems can be avoided by either using Memoization or Tabulation. Let's see how to do it.

Algorithm

  • 1. Initialize a dp array of size n with all values set to 1.
  • 2. Traverse the array from left to right.
  • 3. For each element arr[i], check all previous elements arr[j] (where j < i).
  • 4. If arr[i] > arr[j], update dp[i] = max(dp[i], dp[j] + 1).
  • 5. Finally, return the maximum value in the dp array.

C++ Code

Here is the C++ implementation of the above algorithm. The program takes input as a int vector from the user and displays length of longest subsequence.

#include <iostream>
#include <vector>
using namespace std;

int longestIncreasingSubsequence(vector<int>& nums) {
    int n = nums.size();
    vector<int> dp(n, 1); // Initialize all values to 1

    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            if (nums[i] > nums[j]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
    }

    int maxLen = 0;
    for (int len : dp)
        maxLen = max(maxLen, len);

    return maxLen;
}

int main() {
    vector<int> arr = {10, 22, 9, 33, 21, 50, 41, 60};
    cout << "Length of LIS is " << longestIncreasingSubsequence(arr) << endl;
    return 0;
}

The output of the above code will be:

Length of LIS is 5

Using Binary Search to Find Length of Longest Subsequence

In this method, we have used lower bound of array and dynamic programming reduce the time complexity of the above method. This method will run in O(n log n) time.

Algorithm

  • 1. Maintain an array temp which will hold the end elements of potential increasing subsequences.
  • 2. For each element in the array: Use lower_bound() to find the index in temp where the element can be placed. If the element is larger than all elements in temp, append it. Else, replace the element at the found index.
  • 3. The length of temp will be the length of LIS.

C++ Code

Here is the C++ implementation of the above algorithm. The program takes input as a int vector from the user and displays length of longest subsequence.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int longestIncreasingSubsequence(vector<int>& nums) {
    vector<int> temp;

    for (int num : nums) {
        auto it = lower_bound(temp.begin(), temp.end(), num);
        if (it == temp.end())
            temp.push_back(num);
        else
            *it = num;
    }

    return temp.size();
}

int main() {
    vector<int> arr = {10, 22, 9, 33, 21, 50, 41, 60};
    cout << "Length of LIS is " << longestIncreasingSubsequence(arr) << endl;
    return 0;
} 

The output of the above code will be:

Length of LIS is 5
Updated on: 2025-04-30T20:33:38+05:30

910 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements