Count permutation such that sequence is non decreasing

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++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
#define MAX 20Â
// To store the factorialsint fact[MAX];Â
// Function to update fact[] array// such that fact[i] = i!void pre(){Â
    // 0! = 1    fact[0] = 1;    for (int i = 1; i < MAX; i++) {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsint CountPermutation(int a[], int n){Â
    // To store the result    int ways = 1;Â
    // Sort the array    sort(a, a + n);Â
    // Initial size of the block    int size = 1;    for (int i = 1; i < n; i++) {Â
        // Increase the size of block        if (a[i] == a[i - 1]) {            size++;        }        else {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver codeint main(){Â
    int a[] = { 1, 2, 4, 4, 2, 4 };    int n = sizeof(a) / sizeof(a[0]);Â
    // Pre-calculating factorials    pre();Â
    cout << CountPermutation(a, n);Â
    return 0;} |
Java
//Java implementation of the approachimport java.util.Arrays; import java.io.*;Â
class GFG {static int MAX = 20;Â
// To store the factorialsstatic int []fact=new int[MAX];Â
// Function to update fact[] array// such that fact[i] = i!static void pre(){Â
    // 0! = 1    fact[0] = 1;    for (int i = 1; i < MAX; i++)    {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsstatic int CountPermutation(int a[], int n){Â
    // To store the result    int ways = 1;Â
    // Sort the array    Arrays.sort(a);Â
    // Initial size of the block    int size = 1;    for (int i = 1; i < n; i++)     {Â
        // Increase the size of block        if (a[i] == a[i - 1])         {            size++;        }        else        {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver Codepublic static void main (String[] args) {    int a[] = { 1, 2, 4, 4, 2, 4 };    int n = a.length;         // Pre-calculating factorials    pre();         System.out.println (CountPermutation(a, n));}}Â
// This code is contributed by Sachin |
Python3
# Python3 implementation of the approach MAX = 20Â
# To store the factorials fact = [0] * MAX; Â
# Function to update fact[] array # such that fact[i] = i! def pre() :Â
    # 0! = 1     fact[0] = 1;     for i in range(1, MAX):Â
        # i! = i * (i - 1)!         fact[i] = i * fact[i - 1]; Â
# Function to return the count # of possible permutations def CountPermutation(a, n): Â
    # To store the result     ways = 1; Â
    # Sort the array     a.sort();Â
    # Initial size of the block     size = 1;     for i in range(1, n):Â
        # Increase the size of block         if (a[i] == a[i - 1]):            size += 1;                  else :Â
            # Update the result for             # the previous block             ways *= fact[size]; Â
            # Reset the size to 1             size = 1; Â
    # Update the result for     # the last block     ways *= fact[size]; Â
    return ways; Â
# Driver code if __name__ == "__main__" : Â
    a = [ 1, 2, 4, 4, 2, 4 ];     n = len(a); Â
    # Pre-calculating factorials     pre(); Â
    print(CountPermutation(a, n));      # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â
class GFG{Â Â Â Â Â static int MAX = 20;Â
// To store the factorialsstatic int []fact = new int[MAX];Â
// Function to update fact[] array// such that fact[i] = i!static void pre(){Â
    // 0! = 1    fact[0] = 1;    for (int i = 1; i < MAX; i++)    {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsstatic int CountPermutation(int []a, int n){Â
    // To store the result    int ways = 1;Â
    // Sort the array    Array.Sort(a);Â
    // Initial size of the block    int size = 1;    for (int i = 1; i < n; i++)     {Â
        // Increase the size of block        if (a[i] == a[i - 1])         {            size++;        }        else        {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver Codestatic public void Main (){    int []a = { 1, 2, 4, 4, 2, 4 };    int n = a.Length;         // Pre-calculating factorials    pre();         Console.Write(CountPermutation(a, n));}}Â
// This code is contributed by Sachin. |
Javascript
<script>Â
// Javascript implementation of the approachÂ
const MAX = 20;Â
// To store the factorialslet fact = new Array(MAX);Â
// Function to update fact[] array// such that fact[i] = i!function pre(){Â
    // 0! = 1    fact[0] = 1;    for (let i = 1; i < MAX; i++) {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsfunction CountPermutation(a, n){Â
    // To store the result    let ways = 1;Â
    // Sort the array    a.sort();Â
    // Initial size of the block    let size = 1;    for (let i = 1; i < n; i++) {Â
        // Increase the size of block        if (a[i] == a[i - 1]) {            size++;        }        else {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver codeÂ
    let a = [ 1, 2, 4, 4, 2, 4 ];    let n = a.length;Â
    // Pre-calculating factorials    pre();Â
    document.write(CountPermutation(a, n));Â
</script> |
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
#define MAX 20Â
// To store the factorialsint fact[MAX];Â
// Function to update fact[] array// such that fact[i] = i!void pre(){Â
    // 0! = 1    fact[0] = 1;    for (int i = 1; i < MAX; i++) {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsint CountPermutation(int a[], int n){Â
    // To store the result    int ways = 1;Â
    // Sort the array    sort(a, a + n);Â
    // Initial size of the block    int size = 1;    for (int i = 1; i < n; i++) {Â
        // Increase the size of block        if (a[i] == a[i - 1]) {            size++;        }        else {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver codeint main(){Â
    int a[] = { 1, 2, 4, 4, 2, 4 };    int n = sizeof(a) / sizeof(a[0]);Â
    // Pre-calculating factorials    pre();Â
    cout << CountPermutation(a, n);Â
    return 0;} |
Java
//Java implementation of the approachimport java.util.Arrays; import java.io.*;Â
class GFG {static int MAX = 20;Â
// To store the factorialsstatic int []fact=new int[MAX];Â
// Function to update fact[] array// such that fact[i] = i!static void pre(){Â
    // 0! = 1    fact[0] = 1;    for (int i = 1; i < MAX; i++)    {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsstatic int CountPermutation(int a[], int n){Â
    // To store the result    int ways = 1;Â
    // Sort the array    Arrays.sort(a);Â
    // Initial size of the block    int size = 1;    for (int i = 1; i < n; i++)     {Â
        // Increase the size of block        if (a[i] == a[i - 1])         {            size++;        }        else        {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver Codepublic static void main (String[] args) {    int a[] = { 1, 2, 4, 4, 2, 4 };    int n = a.length;         // Pre-calculating factorials    pre();         System.out.println (CountPermutation(a, n));}}Â
// This code is contributed by Sachin |
Python3
# Python3 implementation of the approach MAX = 20Â
# To store the factorials fact = [0] * MAX; Â
# Function to update fact[] array # such that fact[i] = i! def pre() :Â
    # 0! = 1     fact[0] = 1;     for i in range(1, MAX):Â
        # i! = i * (i - 1)!         fact[i] = i * fact[i - 1]; Â
# Function to return the count # of possible permutations def CountPermutation(a, n): Â
    # To store the result     ways = 1; Â
    # Sort the array     a.sort();Â
    # Initial size of the block     size = 1;     for i in range(1, n):Â
        # Increase the size of block         if (a[i] == a[i - 1]):            size += 1;                  else :Â
            # Update the result for             # the previous block             ways *= fact[size]; Â
            # Reset the size to 1             size = 1; Â
    # Update the result for     # the last block     ways *= fact[size]; Â
    return ways; Â
# Driver code if __name__ == "__main__" : Â
    a = [ 1, 2, 4, 4, 2, 4 ];     n = len(a); Â
    # Pre-calculating factorials     pre(); Â
    print(CountPermutation(a, n));      # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â
class GFG{Â Â Â Â Â static int MAX = 20;Â
// To store the factorialsstatic int []fact = new int[MAX];Â
// Function to update fact[] array// such that fact[i] = i!static void pre(){Â
    // 0! = 1    fact[0] = 1;    for (int i = 1; i < MAX; i++)    {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsstatic int CountPermutation(int []a, int n){Â
    // To store the result    int ways = 1;Â
    // Sort the array    Array.Sort(a);Â
    // Initial size of the block    int size = 1;    for (int i = 1; i < n; i++)     {Â
        // Increase the size of block        if (a[i] == a[i - 1])         {            size++;        }        else        {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver Codestatic public void Main (){    int []a = { 1, 2, 4, 4, 2, 4 };    int n = a.Length;         // Pre-calculating factorials    pre();         Console.Write(CountPermutation(a, n));}}Â
// This code is contributed by Sachin. |
Javascript
<script>Â
// Javascript implementation of the approachÂ
const N = 20;Â
// To store the factorialslet fact = new Array(N);Â
// Function to update fact[] array// such that fact[i] = i!function pre(){Â
    // 0! = 1    fact[0] = 1;    for (let i = 1; i < N; i++) {Â
        // i! = i * (i - 1)!        fact[i] = i * fact[i - 1];    }}Â
// Function to return the count// of possible permutationsfunction CountPermutation(a, n){Â
    // To store the result    let ways = 1;Â
    // Sort the array    a.sort();Â
    // Initial size of the block    let size = 1;    for (let i = 1; i < n; i++) {Â
        // Increase the size of block        if (a[i] == a[i - 1]) {            size++;        }        else {Â
            // Update the result for            // the previous block            ways *= fact[size];Â
            // Reset the size to 1            size = 1;        }    }Â
    // Update the result for    // the last block    ways *= fact[size];Â
    return ways;}Â
// Driver codeÂ
    let a = [ 1, 2, 4, 4, 2, 4 ];    let n = a.length;Â
    // Pre-calculating factorials    pre();Â
    document.write(CountPermutation(a, n));Â
</script> |
12
Â
Time Complexity: O(N * logN)
Auxiliary Space: O(MAX), where max is the size of the factorial array.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



