Count permutations of 0 to N-1 with with at least K elements same as positions
Given two integers, N and K, the task is to find the number of permutations of numbers from 0 to N – 1, such that there are at least K positions in the array such that arr[i] = i ( 0 <= i < N ). As the answer can be very large, calculate the result modulo 10^9+7.
Examples:
Input: N = 4, K = 3
Output: 1
Explanation: There is only one permutation [0, 1, 2, 3] such that number of elements with arr[i] = i is K = 3.Input: N = 4, K = 2
Output: 7
Explanation: There are 7 permutations satisfying the condition which are as follow:
- [0, 1, 2, 3]
- [0, 1, 3, 2]
- [0, 3, 2, 1]
- [0, 2, 1, 3]
- [3, 1, 2, 0]
- [2, 1, 0, 3]
- [1, 0, 2, 3]
Naive approach: The basic idea to solve the problem is to firstly find the Permutation of array.
Follow the steps to solve the problem:
- Recursively find all possible permutations and then,
- Check for each of them whether it is following the condition or not.
- Maintain a counter based on that and increment it when the current permutation satisfies the condition.
Below is the implementation of the above approach :
C++
// C++ code for the above approach: #include <bits/stdc++.h>using namespace std; // Recursive function to get the// all permutations of current arrayvoid getPermutations(vector<int>& arr, int index, int k, int& ans){ // Base condition if current index is // greater than or equal to array size if (index >= arr.size()) { // Initialising the variable count int count = 0; // Counting the number of positions // with arr[i] = i in the array for (int i = 0; i < arr.size(); i++) { if (arr[i] == i) { count++; } } // If count is greater than // or equal to k then // increment the ans if (count >= k) { ans++; } return; } // Iterating over the array arr for (int i = index; i < arr.size(); i++) { // Swapping current index with I swap(arr[index], arr[i]); // Calling recursion for current // condition getPermutations(arr, index + 1, k, ans); // Resetting the swapped position. swap(arr[index], arr[i]); }} int numberOfPermutations(long long n, long long k){ // Initializing the variables //'mod' and 'ans'. int mod = 1e9 + 7; int ans = 0; // Initializing the array 'arr'. vector<int> arr; // Pushing numbers in the array. for (int i = 0; i < n; i++) { arr.push_back(i); } // Calling recursive function 'getPermutations' getPermutations(arr, 0, k, ans); // Returning 'ans'. return ans % mod;} // Driver Codeint main(){ long long N = 4; long long K = 2; cout << numberOfPermutations(N, K); return 0;} |
7
Time Complexity: O(N * N!)
- For recursively finding all permutations, there will be N! recursive calls,
- And for each call, there will be a running loop with N iterations.
- Hence, the overall time complexity will be O(N * N!).
Auxiliary Space: O(N)
Efficient Approach: The idea to solve the problem efficiently is by Counting Derangements of the array.
Follow the steps to solve the problem:
- First fix the positions in the array such that arr[i] != i, Say there are ‘M‘ such positions. (0 <= M <= N – K)
- Count the number of permutations with fixed M for that, and simply choose the indices having the arr[i] !=i
- Find this using the simple combination formula NCM.
- Then, construct a permutation for chosen indices such that for every chosen index, arr[i] !=i, this is nothing but the derangements, and find this using an exhaustive search.
- Then do the casework to find the derangements according to the K value.
Below is the implementation of the above approach :
C++
// C++ code for the above approach: #include <bits/stdc++.h>using namespace std; // Driver function to get the// modular addition.int add(long long a, long long b){ int mod = 1e9 + 7; return ((a % mod) + (b % mod)) % mod;} // Driver function to get the// modular multiplication.int mul(long long a, long long b){ int mod = 1e9 + 7; return ((a % mod) * 1LL * (b % mod)) % mod;} // Driver function to get the// modular binary exponentiation.int bin_pow(long long a, long long b){ int mod = 1e9 + 7; a %= mod; long long res = 1; while (b > 0) { if (b & 1) { res = res * 1LL * a % mod; } a = a * 1LL * a % mod; b >>= 1; } return res;} // Driver function to get the// modular division.int reverse(long long x){ int mod = 1e9 + 7; return bin_pow(x, mod - 2);} int numberOfPermutations(long long n, long long k){ // Updating 'k' with 'n - k'. k = n - k; // Initializing the 'ans' by 1. int ans = 1; // Condition when 'k' is 1. if (k == 0 or k == 1) { return ans; } // Adding derangement for 'k' = 2. ans += mul(mul(n, n - 1), reverse(2)); // Condition when 'k' is 2. if (k == 2) { return ans; } // Adding derangement for 'k' = 3. ans += mul(mul(n, mul(n - 1, n - 2)), reverse(3)); // Condition when 'k' is 3. if (k == 3) { return ans; } // Adding derangement for 'k' = 4. int u = mul(n, mul(n - 1, mul(n - 2, n - 3))); ans = add(ans, mul(u, reverse(8))); ans = add(ans, mul(u, reverse(4))); return ans;} // Driver Codeint main(){ long long N = 4; long long K = 2; cout << numberOfPermutations(N, K); return 0;} |
7
Time Complexity: O(Log N)
- For, using the binary exponential to get the inverse modulo of a number
- the overall time complexity will be O( Log N )
Auxiliary Space: O(1)

