# Count all increasing subsequences

• Difficulty Level : Hard
• Last Updated : 25 Jun, 2021

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.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

```Input : arr[] = {1, 2, 3, 4}
Output: 15
There are total increasing subsequences
{1}, {2}, {3}, {4}, {1,2}, {1,3}, {1,4},
{2,3}, {2,4}, {3,4}, {1,2,3}, {1,2,4},
{1,3,4}, {2,3,4}, {1,2,3,4}

Input : arr[] = {4, 3, 6, 5}
Output: 8
Sub-sequences are {4}, {3}, {6}, {5},
{4,6}, {4,5}, {3,6}, {3,5}

Input : arr[] = {3, 2, 4, 5, 4}
Output : 14
Sub-sequences are {3}, {2}, {4}, {3,4},
{2,4}, {5}, {3,5}, {2,5}, {4,5}, {3,2,5}
{3,4,5}, {4}, {3,4}, {2,4}```

Method 1 (Similar to LIS)

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.```

Illustration:

```For example, arr[] = {3, 2, 4, 5, 4}

// There are no smaller elements on left of arr
// and arr
subCount(0) = 1
subCount(1) = 1

// Note that arr and arr are smaller than arr
subCount(2) = 1 + subCount(0) + subCount(1)  = 3

subCount(3) = 1 + subCount(0) + subCount(1) + subCount(2)
= 1 + 1 + 1 + 3
= 6

subCount(3) = 1 + subCount(0) + subCount(1)
= 1 + 1 + 1
= 3

Result = subCount(0) + subCount(1) + subCount(2) + subCount(3)
= 1 + 1 + 3 + 6 + 3
= 14.```

Time Complexity : O(n2
Auxiliary Space : O(n)
Refer this for implementation.

Method 2 (Efficient)

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 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

// Note that here value is used as index to store counts
count += 1 = 1  // i = 0, arr = 3
count += 1 = 1  // i = 1, arr = 2

// Let us compute count for arr which is 4
count += 1 + count + count += 1 + 1 + 1  = 3

// Let us compute count for arr which is 5
count += 1 + count + count + count
+= 1 + 1 + 1 + 3
= 6

// Let us compute count for arr which is 4
count += 1 + count + count
+= 1 + 1 + 1
+= 3
= 3 + 3
= 6

Note that count[] = {0, 0, 1, 1, 6, 6, 0, 0, 0, 0}
Result = count + count + ... + count
= 1 + 1 + 6 + 6 {count = 1, count = 1
count = 6, count = 6}
= 14.```

Below is the implementation of above idea.

## C++

 `// C++ program to count increasing subsequences``// in an array of digits.``#include``using` `namespace` `std;` `// 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 = {0};` `    ``// scan each digit in arr[]``    ``for` `(``int` `i=0; i=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;``}` `// Driver program to run the test case``int` `main()``{``    ``int` `arr[] = {3, 2, 4, 5, 4};``    ``int` `n = ``sizeof``(arr)/``sizeof``(arr);` `    ``cout << countSub(arr,n);``    ``return` `0;``}`

## Java

 `// Java program to count increasing``// subsequences in an array of digits.``import` `java.io.*;` `class` `GFG {` `    ``// Function To Count all the sub-sequences``    ``// possible in which digit is greater than``    ``// all previous digits arr[] is array of n``    ``// digits``    ``static` `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[] = ``new` `int``[``10``]; ` `        ``// 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;``    ``}` `    ``// Driver program to run the test case``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = {``3``, ``2``, ``4``, ``5``, ``4``};``        ``int` `n = arr.length;` `        ``System.out.println(countSub(arr,n));``    ``}``}``// This code is contributed by Prerna Saini`

## Python3

 `# Python3 program to count increasing``# subsequences in an array of digits.` `# Function To Count all the sub-``# sequences possible in which digit``# is greater than all previous digits``# arr[] is array of n digits``def` `countSub(arr, n):` `    ``# count[] array is used to store all``    ``# sub-sequences possible using that``    ``# digit count[] array covers all the``    ``# digit from 0 to 9``    ``count ``=` `[``0` `for` `i ``in` `range``(``10``)]` `    ``# scan each digit in arr[]``    ``for` `i ``in` `range``(n):``    ` `        ``# count all possible sub-sequences by``        ``# the digits less than arr[i] digit``        ``for` `j ``in` `range``(arr[i] ``-` `1``, ``-``1``, ``-``1``):``            ``count[arr[i]] ``+``=` `count[j]` `        ``# store sum of all sub-sequences``        ``# plus 1 in count[] array``        ``count[arr[i]] ``+``=` `1``    `  `    ``# Now sum up the all sequences``    ``# possible in count[] array``    ``result ``=` `0``    ``for` `i ``in` `range``(``10``):``        ``result ``+``=` `count[i]` `    ``return` `result` `# Driver Code``arr ``=` `[``3``, ``2``, ``4``, ``5``, ``4``]``n ``=` `len``(arr)``print``(countSub(arr, n))` `# This code is contributed by Anant Agarwal.`

## C#

 `// C# program to count increasing``// subsequences in an array of digits.``using` `System;``class` `GFG {` `    ``// Function To Count all the sub-sequences``    ``// possible in which digit is greater than``    ``// all previous digits arr[] is array of n``    ``// digits``    ``static` `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 = ``new` `int``; ` `        ``// 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;``    ``}` `    ``// Driver program``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]arr = {3, 2, 4, 5, 4};``        ``int` `n = arr.Length;` `        ``Console.WriteLine(countSub(arr,n));``    ``}``}``// This code is contributed by Anant Agarwal.`

## Javascript

 ``

Output:

`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.

This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.