Find the number of good permutations
Last Updated :
09 Jun, 2022
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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of good permutations
int Permutations(int n, int k)
{
// For m = 0, ans is 1
int ans = 1;
// If k is greater than 1
if (k >= 2)
ans += (n) * (n - 1) / 2;
// If k is greater than 2
if (k >= 3)
ans += (n) * (n - 1) * (n - 2) * 2 / 6;
// If k is greater than 3
if (k >= 4)
ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
return ans;
}
// Driver code
int main()
{
int n = 5, k = 2;
cout << Permutations(n, k);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the count of good permutations
static int Permutations(int n, int k)
{
// For m = 0, ans is 1
int ans = 1;
// If k is greater than 1
if (k >= 2)
ans += (n) * (n - 1) / 2;
// If k is greater than 2
if (k >= 3)
ans += (n) * (n - 1) * (n - 2) * 2 / 6;
// If k is greater than 3
if (k >= 4)
ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 5, k = 2;
System.out.println(Permutations(n, k));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function to return the count
# of good permutations
def Permutations(n, k):
# For m = 0, ans is 1
ans = 1
# If k is greater than 1
if k >= 2:
ans += (n) * (n - 1) // 2
# If k is greater than 2
if k >= 3:
ans += ((n) * (n - 1) *
(n - 2) * 2 // 6)
# If k is greater than 3
if k >= 4:
ans += ((n) * (n - 1) * (n - 2) *
(n - 3) * 9 // 24)
return ans
# Driver code
if __name__ == "__main__":
n, k = 5, 2
print(Permutations(n, k))
# This code is contributed
# by Rituraj Jain
C#
// C# implementation of the above approach.
using System;
class GFG
{
// Function to return the count of good permutations
static int Permutations(int n, int k)
{
// For m = 0, ans is 1
int ans = 1;
// If k is greater than 1
if (k >= 2)
ans += (n) * (n - 1) / 2;
// If k is greater than 2
if (k >= 3)
ans += (n) * (n - 1) * (n - 2) * 2 / 6;
// If k is greater than 3
if (k >= 4)
ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
return ans;
}
// Driver code
public static void Main()
{
int n = 5, k = 2;
Console.WriteLine(Permutations(n, k));
}
}
/* This code contributed by PrinciRaj1992 */
PHP
<?php
// PHP implementation of the approach
// Function to return the count
// of good permutations
function Permutations($n, $k)
{
// For m = 0, ans is 1
$ans = 1;
// If k is greater than 1
if ($k >= 2)
$ans += ($n) * ($n - 1) / 2;
// If k is greater than 2
if ($k >= 3)
$ans += ($n) * ($n - 1) *
($n - 2) * 2 / 6;
// If k is greater than 3
if ($k >= 4)
$ans += ($n) * ($n - 1) * ($n - 2) *
($n - 3) * 9 / 24;
return $ans;
}
// Driver code
$n = 5; $k = 2;
echo(Permutations($n, $k));
// This code contributed by Code_Mech.
?>
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the count of good permutations
function Permutations(n, k)
{
// For m = 0, ans is 1
var ans = 1;
// If k is greater than 1
if (k >= 2)
ans += (n) * (n - 1) / 2;
// If k is greater than 2
if (k >= 3)
ans += (n) * (n - 1) *
(n - 2) * 2 / 6;
// If k is greater than 3
if (k >= 4)
ans += (n) * (n - 1) * (n - 2) *
(n - 3) * 9 / 24;
return ans;
}
// Driver Code
var n = 5, k = 2;
document.write(Permutations(n, k));
// This code is contributed by Khushboogoyal499
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find the good permutation of first N natural numbers Given an integer N, the task is to print a good permutation of first N natural numbers. Let's denote the ith element of the permutation be pi. A good permutation is a permutation such that for all 1 ? i ? N the following equations hold true, ppi = ipi != i Basically above expressions mean, no value
4 min read
Count the number of special permutations Given two positive integers n and k, the task is to count the number of special permutations. A special permutation P is defined as a permutation of first n natural numbers in which there exists at least (n - k) indices such that Pi = i. Prerequisite: Derangements Examples: Input: n = 4, k = 2 Outpu
15+ min read
Distinct permutations of a number Given an integer N, the task is to print all distinct permutations of the number N. Examples: Input: N = 133Output: 133 313 331Explanation:There are a total of 6 permutations, which are [133, 313, 331, 133, 313, 331].Out of all these permutations, distinct permutations are [133, 313, 331]. Input: N
9 min read
Number of permutation with K inversions | Set 2 Given two integers N and K, the task is to count the number of permutations of the first N natural numbers having exactly K inversions. Since the count can be very large, print it modulo 109 + 7. An inversion is defined as a pair a[i], a[j] such that a[i] > a[j] and i < j. Examples: Input: N =
8 min read
Number of Transpositions in a Permutation Cycle notation is a compact way to represent a permutation by breaking it down into cycles. A cycle represents a set of elements that are permuted (or swapped) among each other.Examples:Let us consider the permutation p = [5, 1, 4, 2, 3] of [1, 2, 3, 4, 5], the elements are moved as 1 â 5, 5 â 3, 3
6 min read
Number of palindromic permutations | Set 1 Given string str, find count of all palindromic permutations of it. Examples : Input : str = "gfgf" Output : 2 There are two palindromic permutations fggf and gffg Input : str = "abc" Output : 0 The idea is based on below facts : A string can permute to a palindrome if number of odd occurring charac
8 min read