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
Input: N = 2, K = 5
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
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 position 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 of the approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- In how many ways the ball will come back to the first boy after N turns
- Find the Initial Array from given array after range sum queries
- 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
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Minimum Initial Energy Required To Cross Street
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Find initial integral solution of Linear Diophantine equation if finite solution exists
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Count of right shifts for each array element to be in its sorted position
- 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
- Front and Back Search in unsorted array
- Minimum number of swaps required to sort an array | Set 2
- Minimum number of swaps required to sort an array
- Count Derangements (Permutation such that no element appears in its original position)
- How to position a div at the bottom of its container using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.