-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fba2a7
commit 751f1d6
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|