Find the smallest number with n set and m unset bits
Last Updated :
31 May, 2022
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:
- Calculate num = (1 << (n + m)) – 1. This will produce a number num having (n + m) number of bits and all are set.
- 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++
#include <bits/stdc++.h>
using namespace std;
unsigned int toggleBitsFromLToR(unsigned int n,
unsigned int l,
unsigned int r)
{
if (r < l)
return n;
int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
return (n ^ num);
}
unsigned int smallNumWithNSetAndMUnsetBits(unsigned int n,
unsigned int m)
{
unsigned int num = (1 << (n + m)) - 1;
return toggleBitsFromLToR(num, n, n + m - 1);
}
int main()
{
unsigned int n = 2, m = 2;
cout << smallNumWithNSetAndMUnsetBits(n, m);
return 0;
}
|
Java
class GFG
{
static int toggleBitsFromLToR( int n, int l, int r)
{
if (r < l)
return n;
int num = (( 1 << r) - 1 ) ^ (( 1 << (l - 1 )) - 1 );
return (n ^ num);
}
static int smallNumWithNSetAndMUnsetBits( int n, int m)
{
int num = ( 1 << (n + m)) - 1 ;
return toggleBitsFromLToR(num, n, n + m - 1 );
}
public static void main (String[] args)
{
int n = 2 , m = 2 ;
System.out.println(smallNumWithNSetAndMUnsetBits(n, m));
}
}
|
Python3
def toggleBitsFromLToR(n, l, r):
if (r < l):
return n
num = (( 1 << r) - 1 ) ^ (( 1 << (l - 1 )) - 1 )
return (n ^ num)
def smallNumWithNSetAndMUnsetBits(n, m):
num = ( 1 << (n + m)) - 1
return toggleBitsFromLToR(num, n, n + m - 1 );
n = 2
m = 2
ans = smallNumWithNSetAndMUnsetBits(n, m)
print (ans)
|
C#
using System;
class GFG
{
static int toggleBitsFromLToR( int n, int l, int r)
{
if (r < l)
return n;
int num = ((1 << r) - 1) ^ ((1 << (l - 1)) - 1);
return (n ^ num);
}
static int smallNumWithNSetAndMUnsetBits( int n, int m)
{
int num = (1 << (n + m)) - 1;
return toggleBitsFromLToR(num, n, n + m - 1);
}
public static void Main ()
{
int n = 2, m = 2;
Console.Write(smallNumWithNSetAndMUnsetBits(n, m));
}
}
|
PHP
<?php
function toggleBitsFromLToR( $n , $l , $r )
{
if ( $r < $l )
return $n ;
$num = ((1 << $r ) - 1) ^ ((1 << ( $l - 1)) - 1);
return ( $n ^ $num );
}
function smallNumWithNSetAndMUnsetBits( $n , $m )
{
$num = (1 << ( $n + $m )) - 1;
return toggleBitsFromLToR( $num , $n , $n + $m - 1);
}
$n = 2; $m = 2;
echo smallNumWithNSetAndMUnsetBits( $n , $m );
?>
|
Javascript
<script>
function toggleBitsFromLToR(n, l, r)
{
if (r < l)
return n;
let num = ((1 << r) - 1) ^
((1 << (l - 1)) - 1);
return (n ^ num);
}
function smallNumWithNSetAndMUnsetBits(n, m)
{
let num = (1 << (n + m)) - 1;
return toggleBitsFromLToR(num, n, n + m - 1);
}
let n = 2, m = 2;
document.write(smallNumWithNSetAndMUnsetBits(n, m));
</script>
|
Output:
9
Time Complexity : O(1)
Space Complexity : O(1)
For greater values of n and m, you can use long int and long long int datatypes to generate the required number.
Share your thoughts in the comments
Please Login to comment...