
- 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++ Program to get number at position k after positioning n natural numbers
Suppose we have two numbers n and k. We are determined to rearrange natural numbers. But there are too many natural numbers, so we have decided to start with the first n. Pick the following sequence of numbers: firstly, all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). We have to find which number will stand at the position number k.
Problem Category
Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first, and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can be used. This problem can be solved by some basic logic or a brute-force approach. Follow the following contents to understand the approach better.
So, if the input of our problem is like n = 10; k = 3, then the output will be 5.
Steps
To solve this, we will follow these steps −
n := (n + 1) / 2 return (k * 2 - 1) if (k <= n), otherwise (2 * (k - n))
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n, int k){ n = (n + 1) / 2; return (k <= n ? k * 2 - 1 : 2 * (k - n)); } int main(){ int n = 10; int k = 3; cout << solve(n, k) << endl; }
Input
10, 3
Output
5
- Related Articles
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- PHP program to find the sum of the first n natural numbers who are not powers of a specific number ‘k’
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- C++ Program to get blocks position after right side rotation
- Python Program to Read a Number n and Print the Natural Numbers Summation Pattern
- Golang Program to Read a Number (n) and Print the Natural Numbers Summation Pattern
- K-th smallest element after removing some integers from natural numbers in C++
- Java Program to Display Numbers and Sum of First N Natural Numbers
- Java program to find the sum of n natural numbers
- Java Program to cube sum of first n natural numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Python Program to Get K initial powers of N
- Swift Program to get array elements after N elements
- Golang Program to get array elements after N elements
- Sum of first n natural numbers in C Program
