Given an integer N and there is a hidden permutation (of numbers from 1 to N, each occurring exactly once) that you need to guess. You can do the following:
Choose a number at 1st position:
- If it is correct, you guess the next position.
- If it is wrong, the whole permutation resets and you go back to guessing the first position.
You can perform trial and error to arrive at the correct permutation, you can also use your previous knowledge for the next guesses. i.e if you know the number at first position correctly, and get 2nd position wrong, in the next move you can input the first position correctly and move on to the second position.
Find the minimum number of moves that it would take in the worst case scenario to get the entire permutation correct.
Input: N = 2
You choose 2 for 1st position, and the permutation resets.
You choose 1 for 1st position, the guess is correct and now you are to guess for the 2nd position.
You choose 2 for the 2nd position since that is the only remaining option you have.
Input: N = 3
Approach: To guess the ith position correctly, it would take (n-i) guesses. And for each guess you would need to make total of i moves( (i-1) moves to enter the correct prefix that you already know and 1 move to guess the current one). In the final step, it would take you N more moves to enter the correct permutation.
Below is the implementation of the above approach:
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of given moves required to make N divisible by 25
- Minimum number of moves required to reach the destination by the king in a chess board
- Number of triangles after N moves
- Count number of 1s in the array after N moves
- Minimum number of moves to reach N starting from (1, 1)
- Expected number of moves to reach the end of a board | Dynamic programming
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Minimum number of given powers of 2 required to represent a number
- Count the number of operations required to reduce the given number
- Number of Transpositions in a Permutation
- Number of permutation with K inversions
- Number of distinct permutation a String can have
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Find smallest permutation of given number
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.