Minimum number N such that total set bits of all numbers from 1 to N is at-least X

Given a number X, the task is to find the minimum number N such that total set bits of all numbers from 1 to n is atleast X.

Examples:

Input: x = 5 
Output: 4 
Set bits in 1-> 1
Set bits in 2-> 1
Set bits in 3-> 2 
Set bits in 4-> 1 
Hence first four numbers add upto 5 

Input: x = 20 
Output: 11 

Approach: Use binary search to get the minimum most number whose sum of bits till N is atleast X. At start, low is 0 and high is initialized according to the constraint. Check if count of set bits is atleast X, every time it is, change high to mid-1, else change it to mid+1. Everytime we do high = mid-1, store the minimal of answer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
#define INF 99999
#define size 10
  
// Function to count sum of set bits
// of all numbers till N
int getSetBitsFromOneToN(int N)
{
    int two = 2, ans = 0;
    int n = N;
  
    while (n) {
        ans += (N / two) * (two >> 1);
  
        if ((N & (two - 1)) > (two >> 1) - 1)
            ans += (N & (two - 1)) - (two >> 1) + 1;
  
        two <<= 1;
        n >>= 1;
    }
    return ans;
}
  
// Function to find the minimum number
int findMinimum(int x)
{
    int low = 0, high = 100000;
  
    int ans = high;
  
    // Binary search for the lowest number
    while (low <= high) {
  
        // Find mid number
        int mid = (low + high) >> 1;
  
        // Check if it is atleast x
        if (getSetBitsFromOneToN(mid) >= x) {
            ans = min(ans, mid);
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
  
    return ans;
}
  
// Driver Code
int main()
{
    int x = 20;
    cout << findMinimum(x);
  
return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
  
class solution
{
static int INF = 99999;
static int size = 10;
  
// Function to count sum of set bits
// of all numbers till N
static int getSetBitsFromOneToN(int N)
{
    int two = 2, ans = 0;
    int n = N;
  
    while (n!=0) {
        ans += (N / two) * (two >> 1);
  
        if ((N & (two - 1)) > (two >> 1) - 1)
            ans += (N & (two - 1)) - (two >> 1) + 1;
  
        two <<= 1;
        n >>= 1;
    }
    return ans;
}
  
// Function to find the minimum number
static int findMinimum(int x)
{
    int low = 0, high = 100000;
  
    int ans = high;
  
    // Binary search for the lowest number
    while (low <= high) {
  
        // Find mid number
        int mid = (low + high) >> 1;
  
        // Check if it is atleast x
        if (getSetBitsFromOneToN(mid) >= x) {
            ans = Math.min(ans, mid);
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
  
    return ans;
}
  
// Driver Code
public static void main(String args[])
{
    int x = 20;
    System.out.println(findMinimum(x));
  
}
  
}
  
//This code is contributed by 
// Shashank_Sharma

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the 
# above approach
INF = 99999
size = 10
  
# Function to count sum of set bits
# of all numbers till N
def getSetBitsFromOneToN(N):
  
    two, ans = 2, 0
    n = N
  
    while (n > 0):
        ans += (N // two) * (two >> 1)
  
        if ((N & (two - 1)) > (two >> 1) - 1):
            ans += (N & (two - 1)) - (two >> 1) + 1
  
        two <<= 1
        n >>= 1
    return ans
  
# Function to find the minimum number
def findMinimum(x):
  
    low = 0
    high = 100000
  
    ans = high
  
    # Binary search for the lowest number
    while (low <= high): 
  
        # Find mid number
        mid = (low + high) >> 1
  
        # Check if it is atleast x
        if (getSetBitsFromOneToN(mid) >= x):
  
            ans = min(ans, mid)
            high = mid - 1
        else:
            low = mid + 1
      
    return ans
  
# Driver Code
x = 20
print(findMinimum(x))
  
# This code is contributed by
# Mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Csharp implementation of the above approach 
using System ;
  
class solution 
static int INF = 99999; 
static int size = 10; 
  
// Function to count sum of set bits 
// of all numbers till N 
static int getSetBitsFromOneToN(int N) 
    int two = 2, ans = 0; 
    int n = N; 
  
    while (n!=0) { 
        ans += (N / two) * (two >> 1); 
  
        if ((N & (two - 1)) > (two >> 1) - 1) 
            ans += (N & (two - 1)) - (two >> 1) + 1; 
  
        two <<= 1; 
        n >>= 1; 
    
    return ans; 
  
// Function to find the minimum number 
static int findMinimum(int x) 
    int low = 0, high = 100000; 
  
    int ans = high; 
  
    // Binary search for the lowest number 
    while (low <= high) { 
  
        // Find mid number 
        int mid = (low + high) >> 1; 
  
        // Check if it is atleast x 
        if (getSetBitsFromOneToN(mid) >= x) { 
            ans = Math.Min(ans, mid); 
            high = mid - 1; 
        
        else
            low = mid + 1; 
    
  
    return ans; 
  
    // Driver Code 
    public static void Main() 
    
        int x = 20; 
        Console.WriteLine(findMinimum(x)); 
      
    
    // This code is contributed by Ryuga

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the above approach
  
// Function to count sum of set bits
// of all numbers till N
function getSetBitsFromOneToN($N)
{
    $two = 2;
    $ans = 0;
    $n = $N;
  
    while ($n)
    {
        $ans += (int)($N / $two) * ($two >> 1);
  
        if (($N & ($two - 1)) > ($two >> 1) - 1)
            $ans += ($N & ($two - 1)) - 
                          ($two >> 1) + 1;
  
        $two <<= 1;
        $n >>= 1;
    }
    return $ans;
}
  
// Function to find the minimum number
function findMinimum($x)
{
    $low = 0;
    $high = 100000;
  
    $ans = $high;
  
    // Binary search for the lowest number
    while ($low <= $high
    {
  
        // Find mid number
        $mid = ($low + $high) >> 1;
  
        // Check if it is atleast x
        if (getSetBitsFromOneToN($mid) >= $x
        {
            $ans = min($ans, $mid);
            $high = $mid - 1;
        }
        else
            $low = $mid + 1;
    }
  
    return $ans;
}
  
// Driver Code
$x = 20;
echo findMinimum($x);
  
// This code is contributed 
// by Sach_Code
?>

chevron_right


Output:

11

Time Complexity: O(log N * log N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.