- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to Find the Longest Increasing Subsequence of a Given Sequence

Longest Increasing Subsequence is a subsequence where one item is greater than its previous item.

Here we will try to find Longest Increasing Subsequence length, from a set of integers.

Input: A set of integers. {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} Output: The length of longest increasing subsequence. Here it is 6. The subsequence is 0, 2, 6, 9, 13, 15.

## Algorithm

### longestSubSeq(subarray, n)

**Input**: The sub array and the size of sub array.

**Output**: Longest increasing sub sequence length.

Begin define array length of size n initially set 0 to all entries of length for i := 1 to n-1, do for j := 0 to i-1, do if subarray[j] < subarray[i] and length[j] > length[i], then length[i] := length[j] done increase length[i] by 1 done lis := 0 for i := 0 to n-1, do lis := maximum of lis and length[i] done return lis End

## Example Code

#include <iostream> using namespace std; int longestSubSeq(int subArr[], int n) { int length[n] = { 0 }; //set all length to 0 length[0] = 1; //subsequence ending with subArr[0] is 1 for (int i = 1; i < n; i++) { //ignore first character, second to all for (int j = 0; j < i; j++) { //subsequence ends with subArr[j] if (subArr[j] < subArr[i] && length[j] > length[i]) length[i] = length[j]; } length[i]++; //add arr[i] } int lis = 0; for (int i = 0; i<n; i++) // find longest increasing subsequence lis = max(lis, length[i]); return lis; } int main() { int arr[] = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 }; int n = 16; cout << "Length of Longest Increasing Subsequence is: " << longestSubSeq(arr, n); return 0; }

## Output

Length of Longest Increasing Subsequence is: 6

- Related Questions & Answers
- Longest Increasing Subsequence
- Program to find length of longest increasing subsequence in Python
- Java Program for Longest Increasing Subsequence
- C++ Program to Find the Longest Prefix Matching of a Given Sequence
- Program to find length of longest circular increasing subsequence in python
- Number of Longest Increasing Subsequence in C++
- Longest Continuous Increasing Subsequence in C++
- Longest Increasing Subsequence in Python
- Program to find length of longest arithmetic subsequence of a given list in Python
- C++ Program to get length of longest subsequence in n times copied sequence
- Program to find length of longest common subsequence in C++
- Program to find length of longest bitonic subsequence in C++
- Program to find length of longest Fibonacci subsequence from a given list in Python
- Program to find length of longest alternating subsequence from a given list in Python
- How to find the length of the longest continuous increasing subsequence from an array of numbers using C#?

Advertisements