Given an integer array ‘arr’ of length ‘n’, return the number of longest increasing subsequences in it.
The longest increasing subsequence(LIS) is the longest subsequence of the given sequence such that all subsequent elements are in strictly increasing order.
Input: ‘n’ = 5, ‘arr’ = [50, 3, 90, 60, 80].
Output: 2
Explanation:
In this array, the longest increasing subsequences are [50, 60, 80] and [3, 60, 80].
There are other increasing subsequences as well, but we need the number of the longest ones. Hence the answer is 2.
The first line of each test case contains a single integer ‘n’ denoting the length of the array ‘arr’.
The following line contains ‘n’ space-separated integers representing elements in ‘arr’.
Output Format :
For each test case, return an integer representing the number of longest increasing subsequences.
Note :
You are not required to print anything. It has already been taken care of. Just implement the function.
4
3 7 4 6
1
The length of LIS is 3, and there is only one such LIS, which is [3, 4, 6].
4
5 7 2 3
2
The length of LIS is 2, and there are two such LIS, which are [5, 7] and [2, 3].
The expected time complexity is O(n^2).
1 ≤ ‘n’ ≤ 5000
1 ≤ ‘arr’[i] ≤ 10 ^ 6
Time limit: 1 sec
Generate all possible subsequences present in the array and count the increasing subsequences of maximum length.
The simplest approach is to generate all possible subsequences present in the given array ‘arr’ and count the increasing subsequences of maximum length. Print the count after checking all subsequences.
For every element in the array, there are two choices, either to include it in the subsequence or not include it. Apply this for every element in the array starting from index 0 until we reach the last index. After we reach the last index, we check if this is an increasing subsequence and if so, then we increment the count of the size of this subsequence by 1.
At last, we simply traverse the count array and find the frequency of the max count.
The steps are as follows :
O( n * (2 ^ n ) ), where n is the length of the array ‘arr’.
We generate ~( 2 ^ n) subsequences. For each subsequence generated we take ~O( n ) time to verify if it is an increasing subsequence.
Hence the time complexity is O( n * (2 ^ n ) ).
O( n ), where n is the length of the array ‘arr’.
‘count’ array requires O ( n ) space. Also, we use the array ‘subArr’ in which at max ~( n ) elements can be there.
Hence the space complexity is O( n ).