Count of Subsequences with distinct elements
Last Updated :
29 Oct, 2023
Given an array arr[] (1<=a[i]<=1e9) containing N (1<=N<=1e5) elements, the task is to find the total number of subsequences such that all elements in that subsequences are distinct. Since the answer can be very large print and modulo 1e9+7.
Examples:
Input: arr[] = [1, 1, 2, 2]
Output: 8
Explanation: The subsequences are [1], [1], [2], [2], [1, 2], [1, 2], [1, 2], [1, 2]
Input: arr[] = [1, 3, 2, 2]
Output: 11
Explanation: The subsequences are [1], [3], [2], [2], [1, 3], [1, 2], [1, 2], [3, 2], [3, 2], [1, 3, 2], [1, 3, 2]
Approach: To solve the problem follow the below idea:
This problem can be solved using unordered_map.
Follow the steps to solve the problem:
- First, let's store the frequency of all elements in a map
- Suppose an element has a frequency of x then we have to either choose one element from this or exclude this element.
- Remember we need all distinct elements so we can't choose more than 1 same element.
- So for an element with frequency x, we have (x+1) options (1 for excluding the elements)
- Now for counting the number of subsequences, we can multiply the options that we have.
- At last, we need to subtract 1 because one empty subsequence is generated so to exclude that we need to subtract 1.
Below is the code for the above approach:
C++
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
// Function to calculate subsequences with
// distinct elements
void distSubs(vector<int> v, int n)
{
long long ans = 1;
// Creating a map
unordered_map<int, int> m;
// Calculating frequency of each element
for (int i = 0; i < n; i++) {
m[v[i]]++;
}
// Calculating ans
for (auto it : m) {
ans = (ans % mod * (it.second + 1) % mod) % mod;
}
ans = (ans - 1 + mod) % mod;
cout << "Total subsequences with all elements distinct "
"are : "
<< ans << endl;
}
// Drivers code
int main()
{
vector<int> v = { 1, 1, 1, 3, 3, 3 };
int n = v.size();
// Function Call
distSubs(v, n);
return 0;
}
Java
import java.util.HashMap;
import java.util.Map;
public class DistinctSubsequences {
private static final int MOD = 1000000007;
// Function to calculate subsequences with distinct elements
private static void distSubs(int[] arr, int n) {
long ans = 1;
// Creating a map to store element frequencies
Map<Integer, Integer> frequencyMap = new HashMap<>();
// Calculating frequency of each element
for (int i = 0; i < n; i++) {
frequencyMap.put(arr[i], frequencyMap.getOrDefault(arr[i], 0) + 1);
}
// Calculating ans
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
int elementFrequency = entry.getValue();
ans = (ans % MOD * (elementFrequency + 1) % MOD) % MOD;
}
ans = (ans - 1 + MOD) % MOD;
System.out.println("Total subsequences with all elements distinct are: " + ans);
}
public static void main(String[] args) {
int[] arr = {1, 1, 1, 3, 3, 3};
int n = arr.length;
// Function Call
distSubs(arr, n);
}
}
Python3
def distSubs(v):
mod = 10**9 + 7
ans = 1
# Creating a dictionary
m = {}
# Calculating frequency of each element
for i in range(len(v)):
m[v[i]] = m.get(v[i], 0) + 1
# Calculating ans
for key, value in m.items():
ans = (ans % mod * (value + 1) % mod) % mod
ans = (ans - 1 + mod) % mod
print("Total subsequences with all elements distinct are:", ans)
# Drivers code
if __name__ == "__main__":
v = [1, 1, 1, 3, 3, 3]
# Function Call
distSubs(v)
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
public class DistinctSubsequences
{
private const int MOD = 1000000007;
// Function to calculate subsequences with distinct elements
private static void DistSubs(int[] arr, int n)
{
long ans = 1;
// Creating a dictionary to store element frequencies
Dictionary<int, int> frequencyMap = new Dictionary<int, int>();
// Calculating frequency of each element
for (int i = 0; i < n; i++)
{
if (frequencyMap.ContainsKey(arr[i]))
{
frequencyMap[arr[i]]++;
}
else
{
frequencyMap[arr[i]] = 1;
}
}
// Calculating ans
foreach (KeyValuePair<int, int> entry in frequencyMap)
{
int elementFrequency = entry.Value;
ans = (ans % MOD * (elementFrequency + 1) % MOD) % MOD;
}
ans = (ans - 1 + MOD) % MOD;
Console.WriteLine("Total subsequences with all elements distinct are: " + ans);
}
public static void Main(string[] args)
{
int[] arr = { 1, 1, 1, 3, 3, 3 };
int n = arr.Length;
// Function Call
DistSubs(arr, n);
}
}
// This code is contributed by Tapesh(tapeshdua420)
JavaScript
function distSubs(v) {
const mod = 1e9 + 7;
let ans = 1;
// Creating a map
const map = new Map();
// Calculating frequency of each element
for (let i = 0; i < v.length; i++) {
if (map.has(v[i])) {
map.set(v[i], map.get(v[i]) + 1);
} else {
map.set(v[i], 1);
}
}
// Calculating ans
for (const [key, value] of map) {
ans = (ans % mod * (value + 1) % mod) % mod;
}
ans = (ans - 1 + mod) % mod;
console.log("Total subsequences with all elements distinct are:", ans);
}
// Driver code
const v = [1, 1, 1, 3, 3, 3];
// Function call
distSubsv);
OutputTotal subsequences with all elements distinct are : 15
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count of subsequences having maximum distinct elements Given an arr of size n. The problem is to count all the subsequences having maximum number of distinct elements. Examples: Input : arr[] = {4, 7, 6, 7} Output : 2 The indexes for the subsequences are: {0, 1, 2} - Subsequence is {4, 7, 6} and {0, 2, 3} - Subsequence is {4, 6, 7} Input : arr[] = {9, 6
5 min read
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 t
4 min read
Count Distinct Subsequences Given a string str of length n, your task is to find the count of distinct subsequences of it.Examples: Input: str = "gfg"Output: 7Explanation: The seven distinct subsequences are "", "g", "f", "gf", "fg", "gg" and "gfg" Input: str = "ggg"Output: 4Explanation: The four distinct subsequences are "",
13 min read
Count Subarrays With At Most K Distinct Elements Given an array arr[] of integers and a positive integer k, the goal is to count the total number of subarrays that contain at most k distinct (unique) elements.Examples:Input: arr[] = [1, 2, 2, 3], k = 2 Output: 9Explanation: Subarrays with at most 2 distinct elements are: [1], [2], [2], [3], [1, 2]
9 min read
Count Subarrays With Exactly K Distinct Elements Given an array arr[] and an integer k, the task is to find the count of subarrays such that each subarray has exactly k distinct elements.Examples:Input: arr[] = [1, 2, 2, 3], k = 2 Output: 4 Explanation: Subarrays with exactly 2 distinct elements are: [1, 2], [1, 2, 2] and [2, 3].Input: arr[] = [3,
10 min read
Length of the longest subsequence consisting of distinct elements Given an array arr[] of size N, the task is to find the length of the longest subsequence consisting of distinct elements only. Examples: Input: arr[] = {1, 1, 2, 2, 2, 3, 3} Output: 3 Explanation: The longest subsequence with distinct elements is {1, 2, 3} Input: arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 }
4 min read