Count of subsequences consisting of the same element
Given an array A[] consisting of N integers, the task is to find the total number of subsequence which contain only one distinct number repeated throughout the subsequence.
Examples:
Input: A[] = {1, 2, 1, 5, 2}
Output: 7
Explanation:
Subsequences {1}, {2}, {1}, {5}, {2}, {1, 1} and {2, 2} satisfy the required conditions.
Input: A[] = {5, 4, 4, 5, 10, 4}
Output: 11
Explanation:
Subsequences {5}, {4}, {4}, {5}, {10}, {4}, {5, 5}, {4, 4}, {4, 4}, {4, 4} and {4, 4, 4} satisfy the required conditions.
Approach:
Follow the steps below to solve the problem:
- Iterate over the array and calculate the frequency of each element in a HashMap.
- Traverse the HashMap. For each element, calculate the number of desired subsequences possible by the equation:
Number of subsequences possible by arr[i] = 2freq[arr[i]] – 1
- Calculate the total possible subsequences from the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void CountSubSequence( int A[], int N)
{
int result = 0;
map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[A[i]]++;
}
for ( auto it : mp) {
result
= result + pow (2, it.second) - 1;
}
cout << result << endl;
}
int main()
{
int A[] = { 5, 4, 4, 5, 10, 4 };
int N = sizeof (A) / sizeof (A[0]);
CountSubSequence(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void CountSubSequence( int A[], int N)
{
int result = 0 ;
Map<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
mp.put(A[i], mp.getOrDefault(A[i], 0 ) + 1 );
}
for (Integer it : mp.values())
{
result = result + ( int )Math.pow( 2 , it) - 1 ;
}
System.out.println(result);
}
public static void main(String[] args)
{
int A[] = { 5 , 4 , 4 , 5 , 10 , 4 };
int N = A.length;
CountSubSequence(A, N);
}
}
|
Python3
def CountSubSequence(A, N):
mp = {}
for element in A:
if element in mp:
mp[element] + = 1
else :
mp[element] = 1
result = 0
for key, value in mp.items():
result + = pow ( 2 , value) - 1
print (result)
A = [ 5 , 4 , 4 , 5 , 10 , 4 ]
N = len (A)
CountSubSequence(A, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void CountSubSequence( int []A, int N)
{
int result = 0;
var mp = new Dictionary< int , int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(A[i]))
mp[A[i]] += 1;
else
mp.Add(A[i], 1);
}
foreach ( var it in mp)
{
result = result +
( int )Math.Pow(2, it.Value) - 1;
}
Console.Write(result);
}
public static void Main()
{
int []A = { 5, 4, 4, 5, 10, 4 };
int N = A.Length;
CountSubSequence(A, N);
}
}
|
Javascript
<script>
function CountSubSequence(A, N)
{
var result = 0;
var mp = new Map();
for ( var i = 0; i < N; i++) {
if (mp.has(A[i]))
mp.set(A[i], mp.get(A[i])+1)
else
mp.set(A[i], 1)
}
mp.forEach((value, key) => {
result
= result + Math.pow(2, value) - 1;
});
document.write( result );
}
var A = [5, 4, 4, 5, 10, 4];
var N = A.length;
CountSubSequence(A, N);
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Last Updated :
21 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...