Longest Decreasing Subsequence
You are given an array/list ARR consisting of N integers. Your task is to find the length of the longest decreasing subsequence.
A subsequence is a sequence of numbers obtained by deleting zero or more elements from the array/list, keeping the relative positions of elements same as it was in the initial sequence. A decreasing subsequence is a subsequence in which every element is strictly less than the previous number.
Note:
There can be more than one subsequences with the longest length.
For example:-
For the given array [5, 0, 3, 2, 9], the longest decreasing subsequence is of length 3, i.e. [5, 3, 2]
Note:
Try to solve the problem in O(N log N) time complexity.
Input Format :
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.
The first line of each test case contains an integer ‘N’ representing the size of the array/list.
The second line of each test case contains N single space-separated integers representing the array/list elements.
Output Format :
For each test case, print the integer denoting the length of the longest decreasing subsequence.
Print the output of each test case in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= N <= 5000
1 <= ARR[i] <= 10^9
Time Limit: 1 sec
CodingNinjas
author
2y
Recursive
We can use recursion to solve this problem.
- For each element, there are two possibilities.
- We include the current item in LDS if it is smaller than the previous element and recurse for remaini...read more
CodingNinjas
author
2y
Memoization
In the recursive approach, many recursive calls had to be made again and again with the same set of parameters. This redundancy can be eliminated by storing the results obtained for a parti...read more
CodingNinjas
author
2y
Dynamic Programming
The problem has optimal substructure. That means the problem can be broken down into smaller sub-problems, which can further be divided until the solution becomes trivial.
The solut...read more
CodingNinjas
author
2y
Efficient approach
The idea here is to maintain lists of decreasing sequences. In general, we have a set of active lists of varying length. We are adding an element A[i] to these lists. We can always a...read more
Add answer anonymously...
Top Flipkart Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
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