Given an integer N, the task is to find the Nth natural number with exactly two bits set.
Examples:
Input: N = 4
Output: 9
Explanation:
Numbers with exactly two bits set: 3, 5, 6, 9, 10, 12, …
4th number in this is 9Input: N = 15
Output: 48
Naive Approach:
- Run a loop through all natural numbers, and for each number, check if it has two bits set or not by counting set bits in a number.
- Print the Nth number having two set bits.
Efficient Approach:
- Find the leftmost set bit by finding the partition to which N belongs (Partition ‘i’ has ‘i’ numbers in it).
- To find the other set bit, we’ll have to first find the distance of N from the last number of the previous partition. Based on their difference, we set the corresponding bit.
Note: To set ith bit (i = 0, 1, 2…) in a number K:
k = k | (1<<(i))
Below is the implementation of the above approach:
C++
// C++ Code to find the Nth number// with exactly two bits set#include <bits/stdc++.h>usingnamespacestd;// Function to find the Nth number// with exactly two bits setvoidfindNthNum(longlongintN){longlongintbit_L = 1, last_num = 0;// Keep incrementing until// we reach the partition of 'N'// stored in bit_Lwhile(bit_L * (bit_L + 1) / 2 < N) {last_num = last_num + bit_L;bit_L++;}// set the rightmost bit// based on bit_Rintbit_R = N - last_num - 1;cout << (1 << bit_L) + (1 << bit_R)<< endl;}// Driver codeintmain(){longlongintN = 13;findNthNum(N);return0;}chevron_rightfilter_noneJava
// Java Code to find the Nth number// with exactly two bits setclassGFG{// Function to find the Nth number// with exactly two bits setstaticvoidfindNthNum(intN){intbit_L =1, last_num =0;// Keep incrementing until// we reach the partition of 'N'// stored in bit_Lwhile(bit_L * (bit_L +1) /2< N) {last_num = last_num + bit_L;bit_L++;}// set the rightmost bit// based on bit_Rintbit_R = N - last_num -1;System.out.print((1<< bit_L) + (1<< bit_R)+"\n");}// Driver codepublicstaticvoidmain(String[] args){intN =13;findNthNum(N);}}// This code is contributed by Princi Singhchevron_rightfilter_nonePython3
# Python Code to find the Nth number# with exactly two bits set# Function to find the Nth number# with exactly two bits setdeffindNthNum(N):bit_L=1;last_num=0;# Keep incrementing until# we reach the partition of 'N'# stored in bit_Lwhile(bit_L*(bit_L+1)/2< N):last_num=last_num+bit_L;bit_L+=1;# set the rightmost bit# based on bit_Rbit_R=N-last_num-1;print((1<< bit_L)+(1<< bit_R));# Driver codeif__name__=='__main__':N=13;findNthNum(N);# This code contributed by PrinciRaj1992chevron_rightfilter_noneC#
// C# Code to find the Nth number// with exactly two bits setusingSystem;classGFG{// Function to find the Nth number// with exactly two bits setstaticvoidfindNthNum(intN){intbit_L = 1, last_num = 0;// Keep incrementing until// we reach the partition of 'N'// stored in bit_Lwhile(bit_L * (bit_L + 1) / 2 < N) {last_num = last_num + bit_L;bit_L++;}// set the rightmost bit// based on bit_Rintbit_R = N - last_num - 1;Console.Write((1 << bit_L) + (1 << bit_R)+"\n");}// Driver codepublicstaticvoidMain(String[] args){intN = 13;findNthNum(N);}}// This code is contributed by Princi Singhchevron_rightfilter_noneOutput:36
Time Complexity : O(Partition of Number)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print first n numbers with exactly two set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Sum of numbers with exactly 2 bits set
- Check if all bits can be made same by flipping two consecutive bits
- Print numbers having first and last bits as the only set bits
- Toggle bits of a number except first and last bits
- Minimize product of first N - 1 natural numbers by swapping same positioned bits of pairs
- Find the largest number smaller than integer N with maximum number of set bits
- Check if a number has two adjacent set bits
- Represent n as the sum of exactly k powers of two | Set 2
- Program to count number of set bits in an (big) array
- Find the largest number with n set and m unset bits
- Find the smallest number with n set and m unset bits
- Find a number containing N - 1 set bits at even positions from the right
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Check whether the number has only first and last bits set | Set 2
- Prime Number of Set Bits in Binary Representation | Set 1
- Count of even set bits between XOR of two arrays
- Program to find Nth term in the given Series
- Program to find the Nth term of series -1, 2, 11, 26, 47......
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


