Count all increasing subsequences - GeeksforGeeks
We are given an array of digits (values lie in range from 0 to 9). The task is to count all the sub sequences possible in array such that in each subsequence every digit is greater than its previous digits in the subsequence.
A Simple Solution is to use Dynamic Programming Solution of Longest Increasing Subsequence (LIS) problem. Like LIS problem, we first compute count of increasing subsequences ending at every index. Finally, we return sum of all values (In LCS problem, we return max of all values).
Read full article from Count all increasing subsequences - GeeksforGeeks
We are given an array of digits (values lie in range from 0 to 9). The task is to count all the sub sequences possible in array such that in each subsequence every digit is greater than its previous digits in the subsequence.
A Simple Solution is to use Dynamic Programming Solution of Longest Increasing Subsequence (LIS) problem. Like LIS problem, we first compute count of increasing subsequences ending at every index. Finally, we return sum of all values (In LCS problem, we return max of all values).
// We count all increasing subsequences ending at every // index i subCount(i) = Count of increasing subsequences ending at arr[i]. // Like LCS, this value can be recursively computed subCount(i) = 1 + subCount(j) where j is index of all elements such that arr[j] < arr[i] and j < i. 1 is added as every element itself is a subsequence of size 1. // Finally we add all counts to get the result. Result = ∑ subCount(i) where i varies from 0 to n-1.
Time Complexity : O(n2)
Auxiliary Space : O(n)
Auxiliary Space : O(n)
int countSub( int arr[], int n )
{
int cSub[n];
/* Initialize cSub values for all indexes */
for (int i = 0; i < n; i++ )
cSub[i] = 1;
for (int i = 1; i < n; i++ )
for (int j = 0; j < i; j++ )
if ( arr[i] > arr[j])
cSub[i] += cSub[j];
int result = 0;
for (int i = 0; i < n; i++ )
result += cSub[i];
return result;
}
The above solution doesn't use the fact that we have only 10 possible values in given array. We can use this fact by using an array count[] such that count[d] stores current count digits smaller than d.
For example, arr[] = {3, 2, 4, 5, 4} // We create a count array and initialize it as 0. count[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // Note that here value is used as index to store counts count[3] += 1 = 1 // i = 0, arr[0] = 3 count[2] += 1 = 1 // i = 1, arr[0] = 2 // Let us compute count for arr[2] which is 4 count[4] += 1 + count[3] + count[2] += 1 + 1 + 1 = 3 // Let us compute count for arr[3] which is 5 count[5] += 1 + count[3] + count[2] + count[4] += 1 + 1 + 1 + 3 = 6 // Let us compute count for arr[4] which is 4 count[4] += 1 + count[0] + count[1] += 1 + 1 + 1 += 3 = 3 + 3 = 6 Note that count[] = {0, 0, 1, 1, 6, 6, 0, 0, 0, 0} Result = count[0] + count[1] + ... + count[9] = 1 + 1 + 6 + 6 {count[2] = 1, count[3] = 1 count[4] = 6, count[5] = 6} = 14.
Time Complexity : O(n) Note that the inner loop runs at most 10 times.
Auxiliary Space : O(1) Note that count has at-most 10 elements.
Auxiliary Space : O(1) Note that count has at-most 10 elements.
// Function To Count all the sub-sequences
// possible in which digit is greater than
// all previous digits arr[] is array of n
// digits
int
countSub(
int
arr[],
int
n)
{
// count[] array is used to store all sub-
// sequences possible using that digit
// count[] array covers all the digit
// from 0 to 9
int
count[10] = {0};
// scan each digit in arr[]
for
(
int
i=0; i<n; i++)
{
// count all possible sub-sequences by
// the digits less than arr[i] digit
for
(
int
j=arr[i]-1; j>=0; j--)
count[arr[i]] += count[j];
// store sum of all sub-sequences plus
// 1 in count[] array
count[arr[i]]++;
}
// now sum up the all sequences possible in
// count[] array
int
result = 0;
for
(
int
i=0; i<10; i++)
result += count[i];
return
result;
}