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.

**Examples:**

Input:N = 2

Output:3

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

Output:7

**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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the required moves ` `int` `countMoves(` `int` `n) ` `{ ` ` ` `int` `ct = 0; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `ct += i * (n - i); ` ` ` ` ` `// Final move ` ` ` `ct += n; ` ` ` `return` `ct; ` `} ` ` ` `// Driver Program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `cout << countMoves(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function that returns the required moves ` `static` `int` `countMoves(` `int` `n) ` `{ ` ` ` `int` `ct = ` `0` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `ct += i * (n - i); ` ` ` ` ` `// Final move ` ` ` `ct += n; ` ` ` `return` `ct; ` `} ` ` ` `// Driver Program to test above function ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println( countMoves(n)); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the approach ` ` ` `# Function that returns the ` `# required moves ` `def` `countMoves(n): ` ` ` ` ` `ct ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `ct ` `+` `=` `i ` `*` `(n ` `-` `i) ` ` ` ` ` `# Final move ` ` ` `ct ` `+` `=` `n ` ` ` `return` `ct ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `3` ` ` `print` `(countMoves(n)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function that returns the required moves ` ` ` `static` `int` `countMoves(` `int` `n) ` ` ` `{ ` ` ` `int` `ct = 0; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `ct += i * (n - i); ` ` ` ` ` `// Final move ` ` ` `ct += n; ` ` ` `return` `ct; ` ` ` `} ` ` ` ` ` `// Driver Program to test above function ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(countMoves(n)); ` ` ` `} ` ` ` ` ` `// This code is contributed by Ryuga. ` ` ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function that returns the ` `// required moves ` `function` `countMoves(` `$n` `) ` `{ ` ` ` `$ct` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$ct` `+= ` `$i` `* (` `$n` `- ` `$i` `); ` ` ` ` ` `// Final move ` ` ` `$ct` `+= ` `$n` `; ` ` ` `return` `$ct` `; ` `} ` ` ` `// Driver Code ` `$n` `= 3; ` `echo` `countMoves(` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

7

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.

## Recommended Posts:

- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum number of given moves required to make N divisible by 25
- Count minimum moves required to convert A to B
- Number of triangles after N moves
- Count number of 1s in the array after N moves
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- 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
- Count minimum number of moves to front or end to sort an array
- Count of minimum reductions required to get the required sum K
- Find minimum moves to reach target on an infinite line
- Minimum moves to reach target on a infinite line | Set 2
- Minimum time to reach a point with +t and -t moves at time t
- Find the index of the left pointer after possible moves in the array
- Total position where king can reach on a chessboard in exactly M moves
- Count possible moves in the given direction in a grid
- Total position where king can reach on a chessboard in exactly M moves | Set 2

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.