Given two integers N and K, the task is to find the smallest number greater than N whose Kth bit in its binary representation is set.
Examples:
Input: N = 15, K = 2
Output: 20
Explanation:
The binary representation of (20)10 is (10100)2. The 2nd bit(0-based indexing) from left is set in (20)10. Therefore, 20 is the smallest number greater than 15 having 2nd bit set.
Input: N = 16, K = 3
Output: 24
Naive Approach: The simplest approach is to traverse all numbers starting from N + 1 and for each number, check if its Kth bit is set or not. If such a number is found, then print that number.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
int find_next( int n, int k)
{
int M = n + 1;
while (1) {
if (M & (1ll << k))
break ;
M++;
}
return M;
}
int main()
{
int N = 15, K = 2;
cout << find_next(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int find_next( int n, int k)
{
int M = n + 1 ;
while ( true )
{
if ((M & (1L << k)) > 0 )
break ;
M++;
}
return M;
}
public static void main(String[] args)
{
int N = 15 , K = 2 ;
System.out.print(find_next(N, K));
}
}
|
Python3
def find_next(n, k):
M = n + 1 ;
while ( True ):
if ((M & ( 1 << k)) > 0 ):
break ;
M + = 1 ;
return M;
if __name__ = = '__main__' :
N = 15 ; K = 2 ;
print (find_next(N, K));
|
C#
using System;
class GFG{
static int find_next( int n, int k)
{
int M = n + 1;
while ( true )
{
if ((M & (1L << k)) > 0)
break ;
M++;
}
return M;
}
public static void Main(String[] args)
{
int N = 15, K = 2;
Console.Write(find_next(N, K));
}
}
|
Javascript
<script>
function find_next(n, k)
{
let M = n + 1;
while ( true )
{
if ((M & (1 << k)) > 0)
break ;
M++;
}
return M;
}
let N = 15, K = 2;
document.write(find_next(N, K));
</script>
|
Time Complexity: O(2K)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, there exist two possibilities:
- Kth bit is not set: Observe that bits having positions greater than K will not be affected. Therefore, make the Kth bit set and make all bits on its left 0.
- Kth bit is set: Find the first least significant unset bit and set it. After that, unset all the bits that are on its right.
Follow the steps below to solve the problem:
- Check if the Kth bit of N is set. If found to be true, then find the lowest unset bit and set it and change all the bits to its right to 0.
- Otherwise, set the Kth bit and update all the bits positioned lower than K to 0.
- Print the answer after completing the above steps.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
int find_next( int n, int k)
{
int ans = 0;
if ((n & (1ll << k)) == 0) {
int cur = 0;
for ( int i = 0; i < k; i++) {
if (n & (1ll << i))
cur += 1ll << i;
}
ans = n - cur + (1ll << k);
}
else {
int first_unset_bit = -1, cur = 0;
for ( int i = 0; i < 64; i++) {
if ((n & (1ll << i)) == 0) {
first_unset_bit = i;
break ;
}
else
cur += (1ll << i);
}
ans = n - cur
+ (1ll << first_unset_bit);
if ((ans & (1ll << k)) == 0)
ans += (1ll << k);
}
return ans;
}
int main()
{
int N = 15, K = 2;
cout << find_next(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int find_next( int n,
int k)
{
int ans = 0 ;
if ((n & (1L << k)) == 0 )
{
int cur = 0 ;
for ( int i = 0 ; i < k; i++)
{
if ((n & (1L << i)) > 0 )
cur += 1L << i;
}
ans = ( int )(n - cur + (1L << k));
}
else
{
int first_unset_bit = - 1 , cur = 0 ;
for ( int i = 0 ; i < 64 ; i++)
{
if ((n & (1L << i)) == 0 )
{
first_unset_bit = i;
break ;
}
else
cur += (1L << i);
}
ans = ( int )(n - cur +
(1L << first_unset_bit));
if ((ans & (1L << k)) == 0 )
ans += (1L << k);
}
return ans;
}
public static void main(String[] args)
{
int N = 15 , K = 2 ;
System.out.print(find_next(N, K));
}
}
|
Python3
def find_next(n, k):
ans = 0
if ((n & ( 1 << k)) = = 0 ):
cur = 0
for i in range (k):
if (n & ( 1 << i)):
cur + = 1 << i
ans = n - cur + ( 1 << k)
else :
first_unset_bit, cur = - 1 , 0
for i in range ( 64 ):
if ((n & ( 1 << i)) = = 0 ):
first_unset_bit = i
break
else :
cur + = ( 1 << i)
ans = n - cur + ( 1 << first_unset_bit)
if ((ans & ( 1 << k)) = = 0 ):
ans + = ( 1 << k)
return ans
N, K = 15 , 2
print (find_next(N, K))
|
C#
using System;
class GFG{
static int find_next( int n,
int k)
{
int ans = 0;
if ((n & (1L << k)) == 0)
{
int cur = 0;
for ( int i = 0; i < k; i++)
{
if ((n & (1L << i)) > 0)
cur += ( int )1L << i;
}
ans = ( int )(n - cur + (1L << k));
}
else
{
int first_unset_bit = -1, cur = 0;
for ( int i = 0; i < 64; i++)
{
if ((n & (1L << i)) == 0)
{
first_unset_bit = i;
break ;
}
else
cur +=( int )(1L << i);
}
ans = ( int )(n - cur +
(1L << first_unset_bit));
if ((ans & (1L << k)) == 0)
ans += ( int )(1L << k);
}
return ans;
}
public static void Main(String[] args)
{
int N = 15, K = 2;
Console.Write(find_next(N, K));
}
}
|
Javascript
<script>
function find_next(n , k) {
var ans = 0;
if ((n & (1 << k)) == 0) {
var cur = 0;
for (i = 0; i < k; i++) {
if ((n & (1 << i)) > 0)
cur += 1 << i;
}
ans = parseInt( (n - cur + (1 << k)));
}
else {
var first_unset_bit = -1, cur = 0;
for (i = 0; i < 64; i++) {
if ((n & (1 << i)) == 0) {
first_unset_bit = i;
break ;
}
else
cur += (1 << i);
}
ans = parseInt( (n - cur + (1 << first_unset_bit)));
if ((ans & (1 << k)) == 0)
ans += (1 << k);
}
return ans;
}
var N = 15, K = 2;
document.write(find_next(N, K));
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)