Skip to content

Commit

Permalink
longest Increasing Subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
Amitesh-tiwari committed Apr 6, 2024
1 parent 3fba2a7 commit 751f1d6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions LongestIncreasingSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Problem statement
For a given array with N elements, you need to find the length of
the longest subsequence from the array such that all the elements
of the subsequence are sorted in strictly increasing order.

Strictly Increasing Sequence is when each term in the sequence
is larger than the preceding term.

For example:
[1, 2, 3, 4] is a strictly increasing array, while [2, 1, 4, 3]
is not.

//code

int longestIncreasingSubsequence(int arr[], int n){
int dp[n];
dp[0] = 1;

int ans = 1;

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

return ans;
}

0 comments on commit 751f1d6

Please sign in to comment.