Number of moves required to guess a permutation in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that find the number of moves required to guess a permutation.

You are given a number N, you need to find the moves required to guess the permutation in worst case scenario.

To guess the ith position, we need n - i moves. And for each move, we need i moves. And finally, we need n steps to find the correct permutation.


Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int getNumberMoves(int n) {
   int count = 0;
   for (int i = 1; i <= n; i++) {
      count += i * (n - i);
   count += n;
   return count;
int main() {
   int n = 9;
   cout << getNumberMoves(n) << endl;
   return 0;


If you run the above code, then you will get the following result.



If you have any queries in the tutorial, mention them in the comment section.

Published on 03-Jul-2021 08:13:03