Longest Increasing Subsequence
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.
Input format:
The first line of input contains an integer 'N', representing the size of the array.
The second line of input contains 'N' space-separated integers, representing the elements of the array.
Output Format:
The only output line contains one integer representing the length of the longest increasing subsequence.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given functions.
Input Constraints
1 <= N <= 10^5
-10^5 <= element <= 10^5
Time Limit: 1sec
CodingNinjas
author
2y
Recursive Approach
- We will write a recursive algorithm that will try all the possibilities.
- The argument of recursive function will be the current index and what was the previous number used for LIS. In...read more
CodingNinjas
author
2y
Memoization
- We will write a recursive algorithm that will try all the possibilities and then memorize them.
- The argument of recursive function will be the current index and what was the previous index o...read more
CodingNinjas
author
2y
Iterative DP
- dp[i] stores the max length of the LIS ending at position ‘i’.
- In order to find out dp[i + 1], we need to try to append the current element in every possible increasing subsequence up to th...read more
CodingNinjas
author
2y
Binary Search + DP
- In this approach, we scan the array from left to right. We also make use of a dp array. This dp array is meant to store the increasing subsequence formed by including the currently e...read more
Add answer anonymously...
Top Nagarro Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
Top HR questions asked in Nagarro Associate Software Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app