Count Increasing Subsequences

PROBLEM :
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:
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}
Input:

The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of digits.
The second line contains N space-separated digits.

Output:
Count of all increasing subsequences in given array of digits.

Constraints:
1 = T = 30
1 = N = 500

Example:
Input:
2
5
3 2 4 5 4
3
1 2 4

Output:
14
7

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
long Count_Increasing_Subsequences(int [],int ) ;
int main()
{
int t,no,i ;
int arr[500] ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
   cout<<Count_Increasing_Subsequences(arr,no)<<endl  ;
}
return 0;
}

long Count_Increasing_Subsequences(int arr[],int no)
{
    int i,j ;
    int temp[no] ;
    long sum ;
   
    for(i=0;i<no;i++)
        temp[i]=1 ;
       
    for(i=1;i<no;i++)
    {
        for(j=0;j<i;j++)
            if(arr[i]>arr[j])
                temp[i]+=temp[j] ;
    }
   
    sum=0 ;
    for(i=0;i<no;i++)
        sum=sum+temp[i] ;
   
    return sum ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )