C++ code to count number of times stones can be given

C++Server Side ProgrammingProgramming

Suppose we have a number n. Amal gives some stones to Bimal and he gives stones more than once, but in one move if Amal gives k stones, in the next move he cannot give k stones, so given stones in one move must be different than the previous move. We have to count the number of times Amal can give stones to Bimal.

So, if the input is like n = 4, then the output will be 3, because 1 stone then 2 stones then again 1 stones.

Steps

To solve this, we will follow these steps −

return (n * 2 + 1) / 3

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int n){
   return (n * 2 + 1) / 3;
}
int main(){
   int n = 4;
   cout << solve(n) << endl;
}

Input

4

Output

3
raja
Updated on 30-Mar-2022 12:35:34

Advertisements