📌  相关文章
📜  下一个比N大的数字,N的二进制表示形式仅相差一位

📅  最后修改于: 2021-06-26 10:18:24             🧑  作者: Mango

给定数字N。任务是找到最小的数字,该数字大于N,并且在N的二进制表示形式中只有一位不同。
注意:此处N可能非常大10 ^ 9 例子:

Input : N = 11
Output : The next number is 15
The binary representation of 11 is 1011
So the smallest number greater than 11 
with one bit different is 1111 which is 15.

Input : N = 17
Output : The next number is 19

简单方法:我们将从N + 1开始运行循环,直到找到一个与N完全不同的数字。如果有大量数字,则此过程可能需要很长时间才能处理
高效方法:在高效方法中,我们必须以二进制形式表示数字。现在,仅当我们保持数字N的所有置位不变并将最低的可能的比特(从0切换为1)时,才可能有大于N的数字且只有1位的不同。
让我们以1001为例,它是9的二进制形式
如果将1001的设置位切换为1000或0001,则发现数字为8和1,小于N。而如果将0设置为1,则发现数字为11(1011)或13(1101),因此,如果将可能的最低位从0切换为1,则我们得到的最小可能数大于N,只有1位不同,在这种情况下为11(1011)。
注意:保证输入数字N的所有位都未设置。 N的二进制表示形式中至少有一个未设置的位。
下面是上述方法的实现:

C++
// CPP program to find next greater number than N
// with exactly one bit different in binary
// representation of N
 
#include 
using namespace std;
 
// Function to find next greater number than N
// with exactly one bit different in binary
// representation of N
long long nextGreater(long long N)
{
    long long power_of_2 = 1, shift_count = 0;
 
    // It is guaranteed that there
    // is a bit zero in the number
    while (true) {
        // If the shifted bit is zero then break
        if (((N >> shift_count) & 1) % 2 == 0)
            break;
 
        // increase the bit shift
        shift_count++;
 
        // increase the power of 2
        power_of_2 = power_of_2 * 2;
    }
 
    // set the lowest bit of the number
    return (N + power_of_2);
}
 
// Driver code
int main()
{
    long long N = 11;
 
    // display the next number
    cout << "The next number is = " << nextGreater(N);
 
    return 0;
}


Java
// Java program to find next greater number than N
// with exactly one bit different in binary
// representation of N
 
class GFG{
// Function to find next greater number than N
// with exactly one bit different in binary
// representation of N
static int nextGreater(int N)
{
    int power_of_2 = 1, shift_count = 0;
 
    // It is guaranteed that there
    // is a bit zero in the number
    while (true) {
        // If the shifted bit is zero then break
        if (((N >> shift_count) & 1) % 2 == 0)
            break;
 
        // increase the bit shift
        shift_count++;
 
        // increase the power of 2
        power_of_2 = power_of_2 * 2;
    }
 
    // set the lowest bit of the number
    return (N + power_of_2);
}
 
// Driver code
public static void main(String[]a)
{
    int N = 11;
 
    // display the next number
    System.out.println("The next number is = " + nextGreater(N));
 
}
}


Python3
# Python3 program to find next greater
# number than N with exactly one
# bit different in binary
# representation of N
 
# Function to find next greater
# number than N with exactly
# one bit different in binary
# representation of N
def nextGreater(N):
 
    power_of_2 = 1;
    shift_count = 0;
 
    # It is guaranteed that there
    # is a bit zero in the number
    while (True):
         
        # If the shifted bit is
        # zero then break
        if (((N >> shift_count) & 1) % 2 == 0):
            break;
 
        # increase the bit shift
        shift_count += 1;
 
        # increase the power of 2
        power_of_2 = power_of_2 * 2;
 
    # set the lowest bit
    # of the number
    return (N + power_of_2);
 
# Driver code
N = 11;
 
# display the next number
print("The next number is =",
             nextGreater(N));
 
# This code is contributed by mits


C#
// C# program to find next
// greater number than N with 
// exactly one bit different in
// binary representation of N
using System;
 
class GFG
{
// Function to find next greater
// number than N with exactly
// one bit different in binary
// representation of N
static int nextGreater(int N)
{
    int power_of_2 = 1,
        shift_count = 0;
 
    // It is guaranteed that there
    // is a bit zero in the number
    while (true)
    {
        // If the shifted bit is
        // zero then break
        if (((N >> shift_count) & 1) % 2 == 0)
            break;
 
        // increase the bit shift
        shift_count++;
 
        // increase the power of 2
        power_of_2 = power_of_2 * 2;
    }
 
    // set the lowest bit
    // of the number
    return (N + power_of_2);
}
 
// Driver code
public static void Main()
{
    int N = 11;
 
    // display the next number
    Console.WriteLine("The next number is = " +
                               nextGreater(N));
}
}
 
// This code is contributed
// by anuj_67


PHP
> $shift_count) & 1) % 2 == 0)
            break;
 
        // increase the bit shift
        $shift_count++;
 
        // increase the power of 2
        $power_of_2 = $power_of_2 * 2;
    }
 
    // set the lowest bit of the number
    return ($N + $power_of_2);
}
 
// Driver code
$N = 11;
 
// display the next number
echo "The next number is = " ,
              nextGreater($N);
 
// This code is contributed
// by anuj_67
?>


Javascript


输出:
The next number is = 15

时间复杂度: O(log(N))

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。