C++ code to find xth element after removing numbers

C++Server Side ProgrammingProgramming

Suppose we have two numbers n and x. First n natural numbers are written on a blackboard. In ith (i starts from 1) operation, we remove ith number from the blackboard. When there are less than i numbers, we stop removal task. We have to find x-th remaining number after stopping removal.

So, if the input is like n = 69; x = 6, then the output will be 12. In first operation, i = 1, so remove 1, then in second operation i = 2, but the sequence is 2, 3, 4 ... so second number is 3, remove 3, like this finally the xth number is 12.

Steps

To solve this, we will follow these steps −

return 2 * x

Example

Let us see the following implementation to get better understanding −

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

Input

69, 6

Output

12
raja
Updated on 15-Mar-2022 06:38:35

Advertisements