Count Subsequences

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
49 upvotes
Asked in companies
OracleHCL TechnologiesHCL Technologies

Problem statement

You have been given an integer array/list 'ARR' of size 'N'. Your task is to return the total number of those subsequences of the array in which all the elements are equal.

A subsequence of a given array is an array generated by deleting some elements of the given array with the order of elements in the subsequence remaining the same as the order of elements in the array.

Note :
As this value might be large, print it modulo 10^9 + 7
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains an integer 'T' which denotes the number of test cases. Then the test cases follow :

The first line of each test case contains an integer 'N' representing the size of the array/list.

The second line contains 'N' single space - separated integers representing the elements of the array.
Output Format :
For each test case, print the total number of subsequences in which all elements are equal.

The output of each test case will be printed in a separate line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 100
1 <= N <= 10^5
0 <= ARR[i] <= 10^9

Time Limit : 1 sec
Sample Input 1 :
2
1
5
2
1 0
Sample Output 1 :
1
2
Explanation For Sample Input 1 :
For the first query with input {5}, the total number of subsequences would be 2 which are {{""}, {5}}.
Out of these subsequences, there is only one subsequence which has all the same elements and that is {5} itself.

For the second query with input {1, 0}, the total number of subsequences would be 4 which are {{""}, {1}, {0}, {1, 0}}.
Out of these subsequences, there are two subsequences which have all the same elements and they are {1}, {0}.
Sample Input 2 :
2
2
1 1
3
1 1 1
Sample Output 2 :
3
7
Hint

Find all the subsequences.

Approaches (2)
Brute Force

The idea is to generate all the subsequences and check whether the elements present are equal or not.

 

Here is the algorithm :

 

  1. Generate all the subsequences of the given array.
  2. Maintain a variable ‘COUNT’ which stores the total number of subsequences in which all the elements are equal.
  3. Iterate over each of the generated subsequences.
    1. In case all the elements of the current subsequence are equal, we increment ‘COUNT’.
  4. After, iterating over all the subsequences, the value of ‘COUNT’ is the answer that we are looking for.
Time Complexity

O((2 ^ N) * N), where ‘N’ is the size of the array.

 

Each number has two choices, whether to include it in a subsequence or not. There are total ‘N’ numbers present with us, so the total possible combinations will be 2*2*2… (‘N’ times). And for every subsequence we check whether all the elements are equal or not by traversing it which can have maximum ‘N’ elements. Therefore, the overall time complexity will be O((2 ^ N) * N).

Space Complexity

O(2 ^ N), where ‘N’ is the size of the array.

 

We use a vector/list to store all the number of subsequences which can be maximum upto 2^N. Recursion stack in our algorithm works in a depth - first way, so we cannot have more than ‘N’ recursive calls on the stack as the total numbers present can be at max ‘N’. Therefore, the overall space complexity will be O(2 ^ N).

Code Solution
(100% EXP penalty)
Count Subsequences
Full screen
Console