Find the number of good permutations
Given two integers N and K. The task is to find the number of good permutations of the first N natural numbers. A permutation is called good if there exist at least N – K indices i (1 ? i ? N) such that Pi = i.
Examples:
Input: N = 4, K = 1
Output: 1
{1, 2, 3, 4} is the only possible good permutation.
Input: N = 5, K = 2
Output: 11
Approach: Let’s iterate on m which is the number of indices such that Pi does not equal i. Obviously, 0 ? m ? k.
In order to count the number of permutations with fixed m, we need to choose the indices that have the property Pi not equals to i – there are nCm ways to do this, then we need to construct a permutation Q for chosen indices such that for every chosen index Qi is not equaled to i. Permutations with this property are called derangements and the number of derangements of fixed size can be calculated using an exhaustive search for m ? 4.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Permutations( int n, int k)
{
int ans = 1;
if (k >= 2)
ans += (n) * (n - 1) / 2;
if (k >= 3)
ans += (n) * (n - 1) * (n - 2) * 2 / 6;
if (k >= 4)
ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
return ans;
}
int main()
{
int n = 5, k = 2;
cout << Permutations(n, k);
return 0;
}
|
Java
class GFG
{
static int Permutations( int n, int k)
{
int ans = 1 ;
if (k >= 2 )
ans += (n) * (n - 1 ) / 2 ;
if (k >= 3 )
ans += (n) * (n - 1 ) * (n - 2 ) * 2 / 6 ;
if (k >= 4 )
ans += (n) * (n - 1 ) * (n - 2 ) * (n - 3 ) * 9 / 24 ;
return ans;
}
public static void main(String[] args)
{
int n = 5 , k = 2 ;
System.out.println(Permutations(n, k));
}
}
|
Python3
def Permutations(n, k):
ans = 1
if k > = 2 :
ans + = (n) * (n - 1 ) / / 2
if k > = 3 :
ans + = ((n) * (n - 1 ) *
(n - 2 ) * 2 / / 6 )
if k > = 4 :
ans + = ((n) * (n - 1 ) * (n - 2 ) *
(n - 3 ) * 9 / / 24 )
return ans
if __name__ = = "__main__" :
n, k = 5 , 2
print (Permutations(n, k))
|
C#
using System;
class GFG
{
static int Permutations( int n, int k)
{
int ans = 1;
if (k >= 2)
ans += (n) * (n - 1) / 2;
if (k >= 3)
ans += (n) * (n - 1) * (n - 2) * 2 / 6;
if (k >= 4)
ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
return ans;
}
public static void Main()
{
int n = 5, k = 2;
Console.WriteLine(Permutations(n, k));
}
}
|
PHP
<?php
function Permutations( $n , $k )
{
$ans = 1;
if ( $k >= 2)
$ans += ( $n ) * ( $n - 1) / 2;
if ( $k >= 3)
$ans += ( $n ) * ( $n - 1) *
( $n - 2) * 2 / 6;
if ( $k >= 4)
$ans += ( $n ) * ( $n - 1) * ( $n - 2) *
( $n - 3) * 9 / 24;
return $ans ;
}
$n = 5; $k = 2;
echo (Permutations( $n , $k ));
?>
|
Javascript
<script>
function Permutations(n, k)
{
var ans = 1;
if (k >= 2)
ans += (n) * (n - 1) / 2;
if (k >= 3)
ans += (n) * (n - 1) *
(n - 2) * 2 / 6;
if (k >= 4)
ans += (n) * (n - 1) * (n - 2) *
(n - 3) * 9 / 24;
return ans;
}
var n = 5, k = 2;
document.write(Permutations(n, k));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...