Longest Decreasing Subsequence

Moderate
0/80
Average time to solve is 35m
profile
Contributed by
7 upvotes
Asked in companies
CultfitAmazonThought Works

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1:
 3
 5 
 5 1 3 2 4
 5 
 6 8 9 2 1
 5 
 1 2 3 4 5 
Sample Output 1:
 3
 3
 1
Explanation for sample input 1:
For the first test case, the longest decreasing subsequence is [5, 3, 2] of length 3.

For the second test case, the longest decreasing subsequence is [9, 2, 1] of length. Note [8,2,1] and [6,2,1] are of length 3 too.

For the third test case, the longest decreasing subsequence is [1] of length 1. Note [2], [3], [4] and [5] are of length 1 too.
Sample Input 2:
 2
 4 
 63 22 56 94 
 2 
 83 80
Sample Output 2:
 2
 2
Hint

Try to check for each element, whether including it in subsequence will result in a longer decreasing subsequence or not.

Approaches (4)
Recursive

We can use recursion to solve this problem.

  1. For each element, there are two possibilities.
    1. We include the current item in LDS if it is smaller than the previous element and recurse for remaining items.
    2. We exclude the current item from LDS and recurse for remaining items.
  2. Finally, we return the length of LDS we get by including or excluding the current item.
  3. The base case of the recursion would be to return 0 when no items are left.
Time Complexity

O(2^N), where N is the length of the array. 

 

We are exploring all possible subsets of the array and there are 2^N subsets.

Space Complexity

O(N), where N is the length of the array.

 

As stack space of size N required for recursive function calls.Hence the overall complexity will be O(N).

Code Solution
(100% EXP penalty)
Longest Decreasing Subsequence
Full screen
Console