Find the smallest number with n set and m unset bits

Given two non-negative numbers n and m. The problem is to find the smallest number having n number of set bits and m number of unset bits in its binary representation.

Constraints: 1 <= n, 0 <= m, (m+n) <= 31

Note : 0 bits before leading 1 (or leftmost 1) in binary representation are counted

Examples:

Input : n = 2, m = 2
Output : 9
(9)10 = (1001)2
We can see that in the binary representation of 9 
there are 2 set and 2 unsets bits and it is the
smallest number. 

Input : n = 4, m = 1
Output : 23

Approach: Following are the steps:

  1. Calculate num = (1 << (n + m)) – 1. This will produce a number num having (n + m) number of bits and all are set.
  2. Now, toggle bits in the range from n to (n+m-1) in num, i.e, to toggle bits from the rightmost nth bit to the rightmost (n+m-1)th bit and then return the toggled number. Refer this post.

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the smallest number
// with n set and m unset bits
#include <bits/stdc++.h>
  
using namespace std;
  
// function to toggle bits in the given range
unsigned int toggleBitsFromLToR(unsigned int n,
                                unsigned int l,
                                unsigned int r)
{
    // for invalid range
    if (r < l)
        return n;
  
    // calculating a number 'num' having 'r'
    // number of bits and bits in the range l
    // to r are the only set bits
    int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
  
    // toggle bits in the range l to r in 'n'
    // and return the number
    return (n ^ num);
}
  
// function to find the smallest number
// with n set and m unset bits
unsigned int smallNumWithNSetAndMUnsetBits(unsigned int n,
                                           unsigned int m)
{
    // calculating a number 'num' having '(n+m)' bits
    // and all are set
    unsigned int num = (1 << (n + m)) - 1;
  
    // required smallest number
    return toggleBitsFromLToR(num, n, n + m - 1);
}
  
// Driver program to test above
int main()
{
    unsigned int n = 2, m = 2;
    cout << smallNumWithNSetAndMUnsetBits(n, m);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the smallest number
// with n set and m unset bits
  
class GFG 
{
    // Function to toggle bits in the given range
    static int toggleBitsFromLToR(int n, int l, int r)
    {
        // for invalid range
        if (r < l)
            return n;
   
        // calculating a number 'num' having 'r'
        // number of bits and bits in the range l
        // to r are the only set bits
        int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
   
        // toggle bits in the range l to r in 'n'
        // and return the number
        return (n ^ num);
    }
      
    // Function to find the smallest number
    // with n set and m unset bits
    static int smallNumWithNSetAndMUnsetBits(int n, int m)
    {
        // calculating a number 'num' having '(n+m)' bits
        // and all are set
        int num = (1 << (n + m)) - 1;
   
        // required smallest number
        return toggleBitsFromLToR(num, n, n + m - 1);
    }
      
    // driver program
    public static void main (String[] args) 
    {
        int n = 2, m = 2;
        System.out.println(smallNumWithNSetAndMUnsetBits(n, m));
    }
}
  
// Contributed by Pramod Kumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find
# the smallest number with n set
# and m unset bits
  
# function to toggle bits in the
# given range
def toggleBitsFromLToR(n, l, r):
  
    # for invalid range
    if (r < l):
        return n
   
    # calculating a number 'num'
    # having 'r' number of bits
    # and bits in the range l
    # to r are the only set bits
    num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1)
   
    # toggle bits in the range
    # l to r in 'n' and return the number
    return (n ^ num)
  
# function to find the smallest number
# with n set and m unset bits
def smallNumWithNSetAndMUnsetBits(n, m):
  
    # calculating a number 'num' having
    # '(n+m)' bits and all are set
    num = (1 << (n + m)) - 1
   
    # required smallest number
    return toggleBitsFromLToR(num, n, n + m - 1);
  
   
# Driver program to test above
n = 2
m = 2
  
ans = smallNumWithNSetAndMUnsetBits(n, m)
print (ans)
  
# This code is contributed by Saloni Gupta

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find the smallest number
// with n set and m unset bits
using System;
  
class GFG
    // Function to toggle bits in the given range
    static int toggleBitsFromLToR(int n, int l, int r)
    {
        // for invalid range
        if (r < l)
            return n;
  
        // calculating a number 'num' having 'r'
        // number of bits and bits in the range l
        // to r are the only set bits
        int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
  
        // toggle bits in the range l to r in 'n'
        // and return the number
        return (n ^ num);
    }
      
    // Function to find the smallest number
    // with n set and m unset bits
    static int smallNumWithNSetAndMUnsetBits(int n, int m)
    {
        // calculating a number 'num' having '(n+m)' bits
        // and all are set
        int num = (1 << (n + m)) - 1;
  
        // required smallest number
        return toggleBitsFromLToR(num, n, n + m - 1);
    }
      
    // Driver program
    public static void Main () 
    {
        int n = 2, m = 2;
        Console.Write(smallNumWithNSetAndMUnsetBits(n, m));
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP  implementation to find the smallest number 
// with n set and m unset bits 
  
// function to toggle bits in the given range 
  
function toggleBitsFromLToR($n,$l,$r
    // for invalid range 
    if ($r < $l
        return $n
  
    // calculating a number 'num' having 'r' 
    // number of bits and bits in the range l 
    // to r are the only set bits 
    $num = ((1 << $r) - 1) ^ ((1 << ($l - 1)) - 1); 
  
    // toggle bits in the range l to r in 'n' 
    // and return the number 
    return ($n ^ $num); 
  
// function to find the smallest number 
// with n set and m unset bits 
function smallNumWithNSetAndMUnsetBits($n, $m)
    // calculating a number 'num' having '(n+m)' bits 
    // and all are set 
    $num = (1 << ($n + $m)) - 1; 
  
    // required smallest number 
    return toggleBitsFromLToR($num, $n, $n + $m - 1); 
  
// Driver program to test above 
     $n = 2; $m = 2; 
     echo  smallNumWithNSetAndMUnsetBits($n, $m); 
  
  
// This Code is Contributed by ajit
?>

chevron_right



Output:

9

For greater values of n and m, you can use long int and long long int datatypes to generate the required number.

This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.