
- 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
C++ code to find xth element after removing numbers
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
- Related Articles
- K-th smallest element after removing some integers from natural numbers in C++
- C++ code to find minimal tiredness after meeting
- C++ code to find tree height after n days
- C++ code to find minimum stones after all operations
- Maximize the maximum subarray sum after removing at most one element in C++
- C++ code to find position of students after coding contest
- C++ code to find money after buying and selling shares
- C++ code to find corrected text after double vowel removal
- C++ code to find final number after min max removal game
- C++ code to count numbers after division elements greater than half of array size
- C++ code to find minimum operations to make numbers c and d
- C++ code to find composite numbers whose difference is n
- C++ code to find three numbers whose sum is n
- Program to find string after removing consecutive duplicate characters in Python
- C++ code to find total number of digits in special numbers

Advertisements