Next greater number than N with exactly one bit different in binary representation of N
Given a number N. The task is to find the smallest number which is greater than N and has only one bit different in the binary representation of N.
Note: Here N can be very large 10^9 < N < 10^15.
Examples:
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
Simple Approach: We will run a loop from N+1 until we find a number which has exactly one bit different than N. This process might take a long time to process in case of large numbers
Efficient Approach: In the efficient approach we have to represent the number in its binary form. Now a number greater than N with only 1 bit different is only possible when we keep all the set bits of the number N intact and switch the lowest possible bit which is zero to 1.
Let us take the example, 1001 which is binary form of 9,
If we switch the set bits of 1001 to either 1000 or 0001 then we find that the numbers are 8 and 1 which are less than N. While if we turn the bits which are zero to 1 we find the numbers are 11 (1011) or 13 (1101) so if switch the lowest possible bit which is zero to 1 then we get the smallest possible number greater than N with only 1 bit different, in this case it is 11 (1011).
Note: It is guaranteed that the input number N does not have all of its bits as set. There exists at least one unset bit in the binary representation of N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long nextGreater( long long N)
{
long long power_of_2 = 1, shift_count = 0;
while ( true ) {
if (((N >> shift_count) & 1) % 2 == 0)
break ;
shift_count++;
power_of_2 = power_of_2 * 2;
}
return (N + power_of_2);
}
int main()
{
long long N = 11;
cout << "The next number is = " << nextGreater(N);
return 0;
}
|
Java
class GFG{
static int nextGreater( int N)
{
int power_of_2 = 1 , shift_count = 0 ;
while ( true ) {
if (((N >> shift_count) & 1 ) % 2 == 0 )
break ;
shift_count++;
power_of_2 = power_of_2 * 2 ;
}
return (N + power_of_2);
}
public static void main(String[]a)
{
int N = 11 ;
System.out.println( "The next number is = " + nextGreater(N));
}
}
|
Python3
def nextGreater(N):
power_of_2 = 1 ;
shift_count = 0 ;
while ( True ):
if (((N >> shift_count) & 1 ) % 2 = = 0 ):
break ;
shift_count + = 1 ;
power_of_2 = power_of_2 * 2 ;
return (N + power_of_2);
N = 11 ;
print ( "The next number is =" ,
nextGreater(N));
|
C#
using System;
class GFG
{
static int nextGreater( int N)
{
int power_of_2 = 1,
shift_count = 0;
while ( true )
{
if (((N >> shift_count) & 1) % 2 == 0)
break ;
shift_count++;
power_of_2 = power_of_2 * 2;
}
return (N + power_of_2);
}
public static void Main()
{
int N = 11;
Console.WriteLine( "The next number is = " +
nextGreater(N));
}
}
|
PHP
<?php
function nextGreater( $N )
{
$power_of_2 = 1;
$shift_count = 0;
while (true)
{
if ((( $N >> $shift_count ) & 1) % 2 == 0)
break ;
$shift_count ++;
$power_of_2 = $power_of_2 * 2;
}
return ( $N + $power_of_2 );
}
$N = 11;
echo "The next number is = " ,
nextGreater( $N );
?>
|
Javascript
<script>
function nextGreater(N)
{
var power_of_2 = 1, shift_count = 0;
while ( true ) {
if (((N >> shift_count) & 1) % 2 == 0)
break ;
shift_count++;
power_of_2 = power_of_2 * 2;
}
return (N + power_of_2);
}
var N = 11;
document.write( "The next number is = " + nextGreater(N));
</script>
|
Output:
The next number is = 15
Time Complexity: O(log(N)), where N represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
13 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...