Count of permutations of size 2N with at least N increasing elements – GeeksforGeeks

Given N, the task is to determine the count of all permutations of size 2N having at least N-increasing elements. An element pi of a permutation P is called increasing if pi < pi+1. For example: 

  • Permutation [1, 2, 3, 4] will count because the number of such i that pi < pi+1 equals 3 (i = 1, i = 2, i = 3). 
  • Permutation [3, 2, 1, 4] won’t count, because the number of such i that pi < pi+1 equals 1 (i = 3). 

Examples:

Input: 1
Output: 1
Explanation: N = 1, there is only one permutation of size 2 that have at least 1 increasing elements: [1, 2].
In permutation [1, 2], p1 < p2 , and there is one, i=1, that satisfy the condition. Since, 1 ≥ N this permutation should be counted. 
In permutation [2, 1], p1 > p2 , and there is no increasing element since, 0<N, this permutation should not be counted.

Input: 2
Output: 12
Explanation: N = 2, there are 12 permutations of size 4 that have at least 2 increasing elements. Following are those permutations: [1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [2, 1, 3, 4], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [3, 1, 2, 4], [3, 4, 1, 2], [4, 1, 2, 3].

Naive Approach: The basic way to solve the problem is as follows:

Generate all permutations of size 2N and for each permutation check if it is the required permutation by counting the number of increasing elements in it.

Time Complexity: O(N! * N),  

  • Generating all the permutations of size 2N will take O(N!) time,  
  • Counting the number of increasing elements in each permutation will take O(N) time 
  • Thus, overall the naive solution will take O(N! * N) time.

Auxiliary Space: O(N)

Efficient Approach: The problem can be solved based on the following observation:

Assume a permutation P and let k be the total count of increasing elements in P. Assume a permutation Q obtained by reversing permutation P and let l be the total count of increasing elements in Q. Mathematically: 

if P = [p1, p2, p3, . . ., p2N], then Q = [p2N, p2N-1, p2N-2, . . ., p2, p1]. 
k = ∑[pi−1 < pi] ∀ 2⩽ i ⩽ 2N

It can be observed that:
The number of increasing elements in Q = 2N – 1 – Number of increasing elements in P = l = 2N – 1 – k

This means, that for every permutation P having the number of increasing elements k < N, there exists a corresponding permutation Q, obtained by reversing P that has the number of increasing elements l ≥ N.

From the above observation, it can be concluded out of all the permutations of size 2N, exactly half of them will have the count of increasing elements l >= N. Therefore, the count of required permutations = total permutations / 2 = (2N)!/2.

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

int countPermutations(int N)

{

    int ans = 1;

    

    for (int i = 1; i <= 2 * N; i++)

        ans = ans * i;

  

    return ans / 2;

}

  

int main()

{

    int N = 2;

  

    

    cout << countPermutations(N);

    return 0;

}

Time Complexity: O(N), as we only need to calculate (2N)! which can be calculated in O(N) time
Auxiliary Space: O(1)

 

Stay connected with us on social media platform for instant update click here to join our  Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here 

Read original article here

Denial of responsibility! FineRadar is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – abuse@fineradar.com. The content will be deleted within 24 hours.
countelementsfineradar updateFree Fire Redeem Codesgadget updateGeeksforGeeksIncreasingLatest tech newspermutationssizeTech Headlinestech newsTech News UpdatesTechnologyTechnology News
Comments (0)
Add Comment