
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Number of moves required to guess a permutation in C++
Given a number N, we need to find the moves required to guess the permutation in the worst-case scenario. The number of moves required to guess the permutation will be n!. Let's take an example.
Input
5
Output
129
When we have 5 elements, then we have 5 ways to guess, and 4 ways when we have 4 elements and it continuous till 1.
Algorithm
- Initialise the number n.
- Initialise count to 1.
- Write a loop that iterates from 1 to n.
- Multiply count with the current number.
- Return the count.
Implementation
Following is the implementation of the above algorithm in C++
#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; }
Output
If you run the above code, then you will get the following result.
129
- Related Articles
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Program to find number of expected moves required to win Lotus and Caterpillar game in Python
- Minimum number of given moves required to make N divisible by 25 using C++.
- Guess Number Higher or Lower in C++
- Program to get next integer permutation of a number in C++
- Guess Number Higher or Lower II in C++
- Find smallest permutation of given number in C++
- Minimum number of moves to escape maze matrix in Python
- Minimum number of prefix reversals to sort permutation of first N numbers in C++
- How to create permutation of array with the given number of elements in JavaScript
- Program to find number of magic sets from a permutation of first n natural numbers in Python
- Count the number of operations required to reduce the given number in C++
- Check if any permutation of a large number is divisible by 8 in Python
- Find the Number of permutation with K inversions using C++
- Minimum number of swaps required to sort an array in C++

Advertisements