![]() |
VOOZH | about |
Given an array arr[] of size n, the task is to count the number of longest increasing subsequences present in the given array.
Examples:
Input: arr[] = [2, 2, 2, 2, 2]
Output: 5
Explanation: The length of the longest increasing subsequence is 1, i.e. {2}. Therefore, count of longest increasing subsequences of length 1 is 5.Input: arr[] = [1, 3, 5, 4, 7]
Output: 2
Explanation: The length of the longest increasing subsequence is 4, and there are 2 longest increasing subsequences of length 4, i.e. {1, 3, 4, 7} and {1, 3, 5, 7}.
Table of Content
The simplest approach is to generate all possible subsequences present in the given arrayarr[] and count the increasing subsequences of maximum length. Print the count after checking all subsequences.
The idea is to modify the Longest Increasing Subsequence DP approach. We add an array count[] along with the lis[] array to store counts of LIS's ending with every index and we finally return the sum of all counts having longest LIS.If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
Optimal Substructure:
- For each element arr[i], to determine the LIS length ending at i, we look at all previous elements arr[j] where j < i and arr[j] < arr[i].
- If including arr[i] extends the LIS ending at arr[j], then lis[i] = lis[j] + 1.
- Additionally, we maintain a count[] array, where count[i] holds the number of LIS's ending at index i. If lis[i] extends the LIS length ending at arr[j], we add the count of subsequences ending at arr[j] to count[i].
- At the end, the LIS length will be the maximum value in the lis[] array, and the total number of such subsequences is the sum of count[i] for all indices where lis[i] is equal to this maximum length.
Overlapping Subproblems:
- By building lis[] and count[] iteratively, we avoid recomputing the LIS and count for previously processed indices, which would occur in a recursive approach. This dynamic programming approach efficiently reuses previously computed solutions, making it optimal for overlapping subproblems.
Recurrence Relation:
- For each pair of indices i and j where j < i and arr[j] < arr[i]:
1. if lis[j] + 1 > lis[i]:
lis[i] = lis[j] +1
count[i] = count[j]
2. if lis[j] +1 = lis[i]:
count[i] += count[j]Base Cases:
- For each element i in array: lis[i] = 1, count[i] = 1, these indicate that each element alone can be an increasing subsequence of length 1 with count 1.
4
We can solve this problem using segment trees. The idea is to build a segment tree and then querying for the range [0,arr[i]-1]. The reason is arr[i] can be appended to any number which is less than it to make a increasing subsequence.
For example if arr[i]=5, arr[i] can append after 0,1,2,3,4 to make increasing subsequence. This logic is what we will try to implement using a segment Tree. We will have a segment tree having nodes containing a pair. The pair contains the LIS length, ways ending at node. Our result would be at the root of the tree.
We make the segment Tree using the maximum value in the array. If it is too large, then we need to step it down using RANKING method. For example, {9,1,4,2} and {3,0,2,1} have the same LIS counts. It is because if you observe, the ordering is the same. We can exploit this idea to minimize the space requirement in our segment Tree to just O(4*n), where n is the size of the array.
Let us briefly go over the implementation of ranking arrangement.
Given an array arr = [1, 9, 100, 2, 2], we want to replace each element with its rank based on its position in the sorted list of unique values.
Sort Unique Values: First, create a sorted list of unique values from arr, giving temp = [1, 2, 9, 100].
Map Values to Ranks: Create a mapping of each unique value in temp to its rank (its index in temp). For example: {1: 0, 2: 1, 9: 2, 100: 3}.
Replace Elements: Use this mapping to replace each element in arr with its rank, resulting in arr = [0, 2, 3, 1, 1].
This is our new stepped down array. This technique is very useful when only the ordering of elements matter more than the value of element.
4