Count number of increasing subsequences of size k

Given an array arr[] containing n integers. The problem is to count number of increasing subsequences in the array of size k.

Examples:

Input : arr[] = {2, 6, 4, 5, 7}, 
            k = 3
Output : 5
The subsequences of size '3' are:
{2, 6, 7}, {2, 4, 5}, {2, 4, 7},
{2, 5, 7} and {4, 5, 7}.

Input : arr[] = {12, 8, 11, 13, 10, 15, 14, 16, 20}, 
            k = 4
Output : 39



Approach: The idea is to use Dynamic Programming by define 2D matrix, say dp[][]. dp[i][j] stores the count of increasing subsequences of size i ending with element arr[j]. So dp[i][j] can be defined as:

dp[i][j] = 1, where i = 1 and 1 <= j <= n
dp[i][j] = sum(dp[i-1][j]), where 1 < i <= k, i <= j <= n and arr[m] < arr[j] for (i-1) <= m < j.

C++

// C++ implementation to count number of
// increasing subsequences of size k
#include <bits/stdc++.h>

using namespace std;

// function to count number of increasing
// subsequences of size k
int numOfIncSubseqOfSizeK(int arr[], int n, int k)
{
    int dp[k][n], sum = 0;
    memset(dp, 0, sizeof(dp));

    // count of increasing subsequences of size 1
    // ending at each arr[i]
    for (int i = 0; i < n; i++)
        dp[0][i] = 1;

    // building up the matrix dp[][]
    // Here 'l' signifies the size of
    // increassing subsequnce of size (l+1).
    for (int l = 1; l < k; l++) {

        // for each increasing subsequence of size 'l'
        // ending with element arr[i]
        for (int i = l; i < n; i++) {

            // count of increasing subsequnces of size 'l'
            // ending with element arr[i]
            dp[l][i] = 0;
            for (int j = l - 1; j < i; j++) {
                if (arr[j] < arr[i])
                    dp[l][i] += dp[l - 1][j];
            }
        }
    }

    // sum up the count of increasing subsequences of
    // size 'k' ending at each element arr[i]
    for (int i = k - 1; i < n; i++)
        sum += dp[k - 1][i];

    // required number of increasing
    // subsequences of size k
    return sum;
}

// Driver program to test above
int main()
{
    int arr[] = { 12, 8, 11, 13, 10, 15, 14, 16, 20 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 4;

    cout << "Number of Increasing Subsequences of size "
         << k << " = " << numOfIncSubseqOfSizeK(arr, n, k);

    return 0;
}

PHP

<?php
// PHP implementation to count 
// number of increasing 
// subsequences of size k

// function to count number
// of increasing subsequences
// of size k
function numOfIncSubseqOfSizeK($arr, 
                               $n, $k)
{
    $dp = array(array()); 
    $sum = 0;
    $dp = array_fill(0, $n + 1, false);

    // count of increasing 
    // subsequences of size 1
    // ending at each arr[i]
    for ($i = 0; $i < $n; $i++)
        $dp[0][$i] = 1;

    // building up the matrix 
    // dp[][]. Here 'l' signifies 
    // the size of increassing 
    // subsequnce of size (l+1).
    for ($l = 1; $l < $k; $l++)
    {

        // for each increasing 
        // subsequence of size 'l'
        // ending with element arr[i]
        for ($i = $l; $i < $n; $i++)
        {

            // count of increasing 
            // subsequnces of size 'l'
            // ending with element arr[i]
            $dp[$l][$i] = 0;
            for ($j = $l - 1; $j < $i; $j++) 
            {
                if ($arr[$j] < $arr[$i])
                    $dp[$l][$i] += $dp[$l - 1][$j];
            }
        }
    }

    // sum up the count of increasing 
    // subsequences of size 'k' ending
    // at each element arr[i]
    for ($i = $k - 1; $i < $n; $i++)
        $sum += $dp[$k - 1][$i];

    // required number of increasing
    // subsequences of size k
    return $sum;
}

// Driver Code
$arr = array(12, 8, 11, 13, 
             10, 15, 14, 16, 20);
$n = sizeof($arr);
$k = 4;

echo "Number of Increasing ". 
     "Subsequences of size ",
                 $k , " = " , 
     numOfIncSubseqOfSizeK($arr, 
                           $n, $k);

// This code is contributed 
// by akt_mit
?>



Output:

Number of Increasing Subsequences of size 4 = 39

Time Complexity: O(kn2).
Auxiliary Space: O(kn).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : jit_t




Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.5 Average Difficulty : 3.5/5.0
Based on 9 vote(s)






User Actions