# Number of ways in which an item returns back to its initial position in N swaps in array of size K

Given two numbers **K** and **N**, the task is to find the number of ways such that an item at position **i** returns back to its initial position in an array of length **K** in **N** steps, where, in each step the item can be swapped with any other item in K

**Examples:**

Input:N = 2, K = 5

Output:4

Explanation:

For the given K, lets assume there are 5 positions 1, 2, 3, 4, 5. Since it is given in the question that the item is at some initial position B and the final answer for all B’s is same, lets assume that the item is at position 1 in the beginning. Therefore, in 2 steps (N value):

The item can either be placed at position 2 and again at position 1.

The item can either be placed at position 3 and again at position 1.

The item can either be placed at position 4 and again at position 1.

The item can either be placed at position 5 and again at position 1.

Therefore, there are a total of 4 ways. Hence the output is 4.

Input:N = 5, K = 5

Output:204

**Approach:** The idea to solve this problem is to use the concept of combinations. The idea is that at every step, there are **K – 1** possibilities to place the item in the next place. To implement this, an array **F[]** is used where **F[i]** represents the number of ways to place the items at poition 1 for ‘i’th step. Since it is given that the item doesn’t belong to the person to whom it belonged to the previous step, therefore, the number of ways of the previous step has to be subtracted for every step. Therefore, the array F[] can be filled as:

F[i] = (K - 1)^{(i - 1)}- F[i - 1]

Finally, the last element of the array F[] is returned.

Below is the implementation the of the approach:

`// C++ program to find the number of ways ` `// in which an item returns back to its ` `// initial position in N swaps ` `// in an array of size K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define mod 1000000007 ` ` ` `// Function to calculate (x^y)%p in O(log y) ` `long` `long` `power(` `long` `x, ` `long` `y) ` `{ ` ` ` `long` `p = mod; ` ` ` ` ` `// Initialize result ` ` ` `long` `res = 1; ` ` ` ` ` `// Update x if it is more than or ` ` ` `// equal to p ` ` ` `x = x % p; ` ` ` ` ` `while` `(y > 0) { ` ` ` `// If y is odd, multiply ` ` ` `// x with result ` ` ` `if` `(y & 1) ` ` ` `res = (res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `// y = y/2 ` ` ` `y = y >> 1; ` ` ` `x = (x * x) % p; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to return the number of ways ` `long` `long` `solve(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Base case ` ` ` `if` `(n == 1) ` ` ` `return` `0LL; ` ` ` ` ` `// Recursive case ` ` ` `// F(n) = (k-1)^(n-1) - F(n-1). ` ` ` `return` `(power((k - 1), n - 1) % mod ` ` ` `- solve(n - 1, k) + mod) ` ` ` `% mod; ` `} ` ` ` `// Drivers code ` `int` `main() ` `{ ` ` ` `int` `n = 4, k = 5; ` ` ` ` ` `// Function calling ` ` ` `cout << solve(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

52

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Number of shuffles required for each element to return to its initial position
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- In how many ways the ball will come back to the first boy after N turns
- Maximum number formed from array with K number of adjacent swaps allowed
- Minimum number of swaps required to sort an array of first N number
- Find number of ways to form sets from N distinct things with no set of size A or B
- Minimum number of swaps required to sort an array | Set 2
- Minimum number of swaps required to sort an array
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Find the Initial Array from given array after range sum queries
- Number of ways to change the Array such that largest element is LCM of array
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Count of elements that can be deleted without disturbing the mean of the initial array
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Number of ways to choose elements from the array such that their average is K
- Number of ways to divide an array into K equal sum sub-arrays
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Count number of ways to divide an array into two halves with same sum
- Distributing M items in a circle of size N starting from K-th position
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.