
- 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
Find kth smallest number in range [1, n] when all the odd numbers are deleted in C++
In this problem, we are given two integer values n and k. Our task is to find kth smallest number in range [1, n] when all the odd numbers are deleted.
We need to find the kth smallest number in the range [1, n] which contains only even values.
So, from range [1, 5] -> number will be 2, 4.
Let’s take an example to understand the problem,
Input: n = 12, k = 4
Output: 8
Explanation:
Even elements in the range [1, n] : 2, 4, 6, 8, 10, 12
The 4th smallest element is 8.
Solution approach:
The solution is simple as we need to find the kth element from even numbers upto n. This can be easily calculated using the formula,
Element = 2*k.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; int main() { int n = 124, k = 12; if(n > 2*k){ cout<<"kth smallest number is "<<(2 * k); } else cout<<"kth smallest number cannot be found"; return 0; }
Output
kth smallest number is 24
- Related Articles
- Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
- Python program to print all odd numbers in a range
- What are composite numbers? Can a composite number be odd? If yes, write the smallest odd composite number.
- kth smallest/largest in a small range unsorted array in C++
- Kth odd number in an array in C++
- Kth Smallest Number in Multiplication Table in C++
- Count all the numbers in a range with smallest factor as K in C++
- Find the number of divisors of all numbers in the range [1, n] in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Find numbers with K odd divisors in a given range in C++
- Program to find kth smallest n length lexicographically smallest string in python
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
- Program to find kth smallest element in linear time in Python
- C++ program to find numbers with K odd divisors in a given range
- Program to find nearest number of n where all digits are odd in python

Advertisements