Count Permutations in a Sequence
Last Updated :
28 Nov, 2023
Given an array A consisting of N positive integers, find the total number of subsequences of the given array such that the chosen subsequence represents a permutation.
Note:
- Sequence A is a subsequence of B if A can be obtained from B by deleting some(possibly, zero) elements without changing its order. For example, [3,1] is a subsequence of [3,2,1] and [4,3,1], but not a subsequence of [1,3,3,7] and [3,10,4].
- Two permutations are different if they are of different lengths or if there is any element in the two permutations such that its index in the original array is different.
- A permutation of length N is an array of length N in which every element from 1 to N occurs exactly once.
Examples:
Input: N = 5, A[] = {1, 2, 3, 2, 4}
Output: 7
Explanation: We can get 7 permutations: {1}, {1, 2}, {1, 2}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4}.
Input: N = 4, A[] = {2, 1, 1, 2}
Output: 6
Explanation: We can get 6 permutations: {1}, {1}, {2, 1}, {2, 1}, {1, 2}, {1, 2}
Approach: This can be solved with the following idea:
We can use a map to store the frequency of all the elements in the array A[]. Now, run a loop starting from 1 till we get an element whose frequency is 0. The main idea is to calculate the number of permutations which can be formed using first (i-1) elements, then for each of these permutations we can append every i to form a new permutation.
Below are the steps involved:
- Store the frequency of each element in a map, say freq_arr.
- Maintain a variable prev = 1, to store the number of permutations which can be formed using elements from 1 to (i-1)
- Now, start a loop from 1 till we reach a number whose frequency in the original array is 0.
- Maintain a variable ans = 0, to store the final answer.
- For each iteration i,
- Check if freq[i] > 0. If not, break
- Else, Update prev with prev * freq[i] and add prev to ans
- Return ans
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int solve( int N, vector< int >& A)
{
map< int , int > freq_arr;
long long prev = 1;
for ( auto ele : A)
freq_arr[ele]++;
long long ans = 0;
for ( int i = 1; freq_arr[i]; i++) {
prev = prev * freq_arr[i];
ans += prev;
}
return ans;
}
int main()
{
int N = 4;
vector< int > A = { 2, 1, 1, 2 };
cout << solve(N, A);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
public class PermutationCount {
static long solve( int N, ArrayList<Integer> A) {
Map<Integer, Integer> freqMap = new HashMap<>();
long prev = 1 ;
for ( int ele : A) {
freqMap.put(ele, freqMap.getOrDefault(ele, 0 ) + 1 );
}
long ans = 0 ;
for ( int i = 1 ; freqMap.containsKey(i); i++) {
prev *= freqMap.get(i);
ans += prev;
}
return ans;
}
public static void main(String[] args) {
int N = 4 ;
ArrayList<Integer> A = new ArrayList<>();
A.add( 2 );
A.add( 1 );
A.add( 1 );
A.add( 2 );
System.out.println(solve(N, A));
}
}
|
Python3
def solve(N, A):
freq_dict = {}
prev = 1
for ele in A:
freq_dict[ele] = freq_dict.get(ele, 0 ) + 1
ans = 0
for i in range ( 1 , N + 1 ):
if i in freq_dict:
prev = prev * freq_dict[i]
ans + = prev
else :
break
return ans
if __name__ = = "__main__" :
N = 4
A = [ 2 , 1 , 1 , 2 ]
print (solve(N, A))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static long Solve( int N, List< int > A) {
Dictionary< int , int > freqDict = new Dictionary< int , int >();
long prev = 1;
foreach ( var ele in A) {
if (freqDict.ContainsKey(ele))
freqDict[ele]++;
else
freqDict[ele] = 1;
}
long ans = 0;
for ( int i = 1; freqDict.ContainsKey(i); i++) {
prev = prev * freqDict[i];
ans += prev;
}
return ans;
}
public static void Main( string [] args) {
int N = 4;
List< int > A = new List< int > { 2, 1, 1, 2 };
Console.WriteLine(Solve(N, A));
}
}
|
Javascript
function GFG(N, A) {
const freqMap = new Map();
let prev = 1;
for (const ele of A) {
if (freqMap.has(ele)) {
freqMap.set(ele, freqMap.get(ele) + 1);
} else {
freqMap.set(ele, 1);
}
}
let ans = 0;
for (let i = 1; freqMap.has(i); i++) {
prev *= freqMap.get(i);
ans += prev;
}
return ans;
}
const N = 4;
const A = [2, 1, 1, 2];
console.log(GFG(N, A));
|
Time Complexity: O(N), where N is the size of input array A[]
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...