Count permutation such that sequence is non decreasing
Last Updated :
21 Dec, 2022
Given an array arr[] of integers, the task is to find the count of permutation of the array such that the permutation is in increasing order i.e. arr[0] ? arr[1] ? arr[2] ? … ? arr[n – 1].
Examples:
Input: arr[] = {1, 2, 1}
Output: 2
1, 1, 2 and 1, 1, 2 are the only valid permutations.
Input: arr[] = {5, 4, 4, 5}
Output: 4
Approach: The sequence should be non-descending i.e. arr[0] ? arr[1] ? arr[2] ? … ? arr[n – 1].
First, sort the array and then focus on the block where all elements are equal as these elements can be rearranged in P! ways where P is the size of that block.
Permuting that block will not violate the given condition. Now, find all the blocks where all elements are equal and multiply the answer of that individual block to the final answer to get the total count of possible permutations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 20
int fact[MAX];
void pre()
{
fact[0] = 1;
for ( int i = 1; i < MAX; i++) {
fact[i] = i * fact[i - 1];
}
}
int CountPermutation( int a[], int n)
{
int ways = 1;
sort(a, a + n);
int size = 1;
for ( int i = 1; i < n; i++) {
if (a[i] == a[i - 1]) {
size++;
}
else {
ways *= fact[size];
size = 1;
}
}
ways *= fact[size];
return ways;
}
int main()
{
int a[] = { 1, 2, 4, 4, 2, 4 };
int n = sizeof (a) / sizeof (a[0]);
pre();
cout << CountPermutation(a, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.io.*;
class GFG
{
static int MAX = 20 ;
static int []fact= new int [MAX];
static void pre()
{
fact[ 0 ] = 1 ;
for ( int i = 1 ; i < MAX; i++)
{
fact[i] = i * fact[i - 1 ];
}
}
static int CountPermutation( int a[], int n)
{
int ways = 1 ;
Arrays.sort(a);
int size = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (a[i] == a[i - 1 ])
{
size++;
}
else
{
ways *= fact[size];
size = 1 ;
}
}
ways *= fact[size];
return ways;
}
public static void main (String[] args)
{
int a[] = { 1 , 2 , 4 , 4 , 2 , 4 };
int n = a.length;
pre();
System.out.println (CountPermutation(a, n));
}
}
|
Python3
MAX = 20
fact = [ 0 ] * MAX ;
def pre() :
fact[ 0 ] = 1 ;
for i in range ( 1 , MAX ):
fact[i] = i * fact[i - 1 ];
def CountPermutation(a, n):
ways = 1 ;
a.sort();
size = 1 ;
for i in range ( 1 , n):
if (a[i] = = a[i - 1 ]):
size + = 1 ;
else :
ways * = fact[size];
size = 1 ;
ways * = fact[size];
return ways;
if __name__ = = "__main__" :
a = [ 1 , 2 , 4 , 4 , 2 , 4 ];
n = len (a);
pre();
print (CountPermutation(a, n));
|
C#
using System;
class GFG
{
static int MAX = 20;
static int []fact = new int [MAX];
static void pre()
{
fact[0] = 1;
for ( int i = 1; i < MAX; i++)
{
fact[i] = i * fact[i - 1];
}
}
static int CountPermutation( int []a, int n)
{
int ways = 1;
Array.Sort(a);
int size = 1;
for ( int i = 1; i < n; i++)
{
if (a[i] == a[i - 1])
{
size++;
}
else
{
ways *= fact[size];
size = 1;
}
}
ways *= fact[size];
return ways;
}
static public void Main ()
{
int []a = { 1, 2, 4, 4, 2, 4 };
int n = a.Length;
pre();
Console.Write(CountPermutation(a, n));
}
}
|
Javascript
<script>
const MAX = 20;
let fact = new Array(MAX);
function pre()
{
fact[0] = 1;
for (let i = 1; i < MAX; i++) {
fact[i] = i * fact[i - 1];
}
}
function CountPermutation(a, n)
{
let ways = 1;
a.sort();
let size = 1;
for (let i = 1; i < n; i++) {
if (a[i] == a[i - 1]) {
size++;
}
else {
ways *= fact[size];
size = 1;
}
}
ways *= fact[size];
return ways;
}
let a = [ 1, 2, 4, 4, 2, 4 ];
let n = a.length;
pre();
document.write(CountPermutation(a, n));
</script>
|
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 20
int fact[MAX];
void pre()
{
fact[0] = 1;
for ( int i = 1; i < MAX; i++) {
fact[i] = i * fact[i - 1];
}
}
int CountPermutation( int a[], int n)
{
int ways = 1;
sort(a, a + n);
int size = 1;
for ( int i = 1; i < n; i++) {
if (a[i] == a[i - 1]) {
size++;
}
else {
ways *= fact[size];
size = 1;
}
}
ways *= fact[size];
return ways;
}
int main()
{
int a[] = { 1, 2, 4, 4, 2, 4 };
int n = sizeof (a) / sizeof (a[0]);
pre();
cout << CountPermutation(a, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.io.*;
class GFG
{
static int MAX = 20 ;
static int []fact= new int [MAX];
static void pre()
{
fact[ 0 ] = 1 ;
for ( int i = 1 ; i < MAX; i++)
{
fact[i] = i * fact[i - 1 ];
}
}
static int CountPermutation( int a[], int n)
{
int ways = 1 ;
Arrays.sort(a);
int size = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (a[i] == a[i - 1 ])
{
size++;
}
else
{
ways *= fact[size];
size = 1 ;
}
}
ways *= fact[size];
return ways;
}
public static void main (String[] args)
{
int a[] = { 1 , 2 , 4 , 4 , 2 , 4 };
int n = a.length;
pre();
System.out.println (CountPermutation(a, n));
}
}
|
Python3
MAX = 20
fact = [ 0 ] * MAX ;
def pre() :
fact[ 0 ] = 1 ;
for i in range ( 1 , MAX ):
fact[i] = i * fact[i - 1 ];
def CountPermutation(a, n):
ways = 1 ;
a.sort();
size = 1 ;
for i in range ( 1 , n):
if (a[i] = = a[i - 1 ]):
size + = 1 ;
else :
ways * = fact[size];
size = 1 ;
ways * = fact[size];
return ways;
if __name__ = = "__main__" :
a = [ 1 , 2 , 4 , 4 , 2 , 4 ];
n = len (a);
pre();
print (CountPermutation(a, n));
|
C#
using System;
class GFG
{
static int MAX = 20;
static int []fact = new int [MAX];
static void pre()
{
fact[0] = 1;
for ( int i = 1; i < MAX; i++)
{
fact[i] = i * fact[i - 1];
}
}
static int CountPermutation( int []a, int n)
{
int ways = 1;
Array.Sort(a);
int size = 1;
for ( int i = 1; i < n; i++)
{
if (a[i] == a[i - 1])
{
size++;
}
else
{
ways *= fact[size];
size = 1;
}
}
ways *= fact[size];
return ways;
}
static public void Main ()
{
int []a = { 1, 2, 4, 4, 2, 4 };
int n = a.Length;
pre();
Console.Write(CountPermutation(a, n));
}
}
|
Javascript
<script>
const N = 20;
let fact = new Array(N);
function pre()
{
fact[0] = 1;
for (let i = 1; i < N; i++) {
fact[i] = i * fact[i - 1];
}
}
function CountPermutation(a, n)
{
let ways = 1;
a.sort();
let size = 1;
for (let i = 1; i < n; i++) {
if (a[i] == a[i - 1]) {
size++;
}
else {
ways *= fact[size];
size = 1;
}
}
ways *= fact[size];
return ways;
}
let a = [ 1, 2, 4, 4, 2, 4 ];
let n = a.length;
pre();
document.write(CountPermutation(a, n));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(MAX), where max is the size of the factorial array.
Share your thoughts in the comments
Please Login to comment...