给定两个非负数n和m 。问题是找到在其二进制表示中具有n个置位位数和m个未置位位数的最小数字。
约束: 1 <= n,0 <= m,(m + n)<= 31
注意:在二进制表示形式中,前1(或最左1)位之前的0位被计数
例子:
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
方法:以下是步骤:
- 计算num =(1 <<(n + m))–1。这将产生一个具有(n + m)个位数的数字num ,并且全部被设置。
- 现在,在num的n到(n + m-1)范围内切换位,即从最右边的第n位切换到最右边的(n + m-1)位,然后返回切换后的数字。请参阅这篇文章。
C/C++
// C++ implementation to find the smallest number
// with n set and m unset bits
#include
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;
}
Java
// 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
Python3
# 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
C#
// 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
PHP
输出:
9
对于较大的n和m值,可以使用long int和long long int数据类型来生成所需的数字。