Given two integers **N** and **K**, the task is to find the smallest number greater than **N** whose **K ^{th}** bit in its binary representation is set.

**Examples:**

Input:N = 15, K = 2Output:20Explanation:

The binary representation of (20)_{10}is (10100)_{2}. The 2^{nd}bit(0-based indexing) from left is set in (20)_{10}. Therefore, 20 is the smallest number greater than 15 having 2^{nd}bit set.

Input:N = 16, K = 3Output:24

**Naive Approach:** The simplest approach is to traverse all numbers starting from **N + 1** and for each number, check if its **K ^{th}** bit is set or not. If such a number is found, then print that number.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include "bits/stdc++.h"` `using` `namespace` `std;` `// Function to find the number greater` `// than n whose Kth bit is set` `int` `find_next(` `int` `n, ` `int` `k)` `{` ` ` `// Iterate from N + 1` ` ` `int` `M = n + 1;` ` ` `while` `(1) {` ` ` `// Check if Kth bit is` ` ` `// set or not` ` ` `if` `(M & (1ll << k))` ` ` `break` `;` ` ` `// Increment M for next number` ` ` `M++;` ` ` `}` ` ` `// Return the minimum value` ` ` `return` `M;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given N and K` ` ` `int` `N = 15, K = 2;` ` ` `// Function Call` ` ` `cout << find_next(N, K);` ` ` `return` `0;` `}` |

## Java

`// Java program for` `// the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the number greater` `// than n whose Kth bit is set` `static` `int` `find_next(` `int` `n, ` `int` `k)` `{` ` ` `// Iterate from N + 1` ` ` `int` `M = n + ` `1` `;` ` ` `while` `(` `true` `)` ` ` `{` ` ` `// Check if Kth bit is` ` ` `// set or not` ` ` `if` `((M & (1L << k)) > ` `0` `)` ` ` `break` `;` ` ` `// Increment M for` ` ` `// next number` ` ` `M++;` ` ` `}` ` ` `// Return the minimum value` ` ` `return` `M;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given N and K` ` ` `int` `N = ` `15` `, K = ` `2` `;` ` ` `// Function Call` ` ` `System.out.print(find_next(N, K));` `}` `}` `// This code is contributed by Rajput-Ji` |

## Python3

`# Python3 program for` `# the above approach` `# Function to find the number` `# greater than n whose Kth` `# bit is set` `def` `find_next(n, k):` ` ` ` ` `# Iterate from N + 1` ` ` `M ` `=` `n ` `+` `1` `;` ` ` `while` `(` `True` `):` ` ` ` ` `# Check if Kth bit is` ` ` `# set or not` ` ` `if` `((M & (` `1` `<< k)) > ` `0` `):` ` ` `break` `;` ` ` `# Increment M for` ` ` `# next number` ` ` `M ` `+` `=` `1` `;` ` ` `# Return the` ` ` `# minimum value` ` ` `return` `M;` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given N and K` ` ` `N ` `=` `15` `; K ` `=` `2` `;` ` ` `# Function Call` ` ` `print` `(find_next(N, K));` `# This code is contributed by Rajput-Ji` |

## C#

`// C# program for` `// the above approach` `using` `System;` `class` `GFG{` `// Function to find the` `// number greater than n` `// whose Kth bit is set` `static` `int` `find_next(` `int` `n, ` `int` `k)` `{` ` ` `// Iterate from N + 1` ` ` `int` `M = n + 1;` ` ` `while` `(` `true` `)` ` ` `{` ` ` `// Check if Kth bit is` ` ` `// set or not` ` ` `if` `((M & (1L << k)) > 0)` ` ` `break` `;` ` ` `// Increment M for` ` ` `// next number` ` ` `M++;` ` ` `}` ` ` `// Return the minimum value` ` ` `return` `M;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Given N and K` ` ` `int` `N = 15, K = 2;` ` ` `// Function Call` ` ` `Console.Write(find_next(N, K));` `}` `}` `// This code is contributed by Rajput-Ji` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach` ` ` ` ` `// Function to find the number greater` `// than n whose Kth bit is set` `function` `find_next(n, k)` `{` ` ` `// Iterate from N + 1` ` ` `let M = n + 1;` ` ` ` ` `while` `(` `true` `)` ` ` `{` ` ` `// Check if Kth bit is` ` ` `// set or not` ` ` `if` `((M & (1 << k)) > 0)` ` ` `break` `;` ` ` ` ` `// Increment M for` ` ` `// next number` ` ` `M++;` ` ` `}` ` ` ` ` `// Return the minimum value` ` ` `return` `M;` `}` `// Driver Code` ` ` ` ` `// Given N and K` ` ` `let N = 15, K = 2;` ` ` ` ` `// Function Call` ` ` `document.write(find_next(N, K));` ` ` ` ` `// This code is contributed by avijitmondal1998.` `</script>` |

**Output**

20

**Time Complexity:** O(2^{K})**Auxiliary Space:** O(1)

**Efficient Approach:** To optimize the above approach, there exist two possibilities:

**K**Observe that bits having positions greater than^{th}bit is not set:**K**will not be affected. Therefore, make the**K**bit set and make all bits on its left^{th}**0**.**K**Find the first least significant unset bit and set it. After that, unset all the bits that are on its right.^{th }bit is set:

Follow the steps below to solve the problem:

- Check if the
**K**bit of^{th }**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
**K**bit and update all the bits positioned lower than^{th}**K**to**0**. - Print the answer after completing the above steps.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include "bits/stdc++.h"` `using` `namespace` `std;` `// Function to find the number greater` `// than n whose Kth bit is set` `int` `find_next(` `int` `n, ` `int` `k)` `{` ` ` `// Stores the resultant number` ` ` `int` `ans = 0;` ` ` `// If Kth bit is not set` ` ` `if` `((n & (1ll << k)) == 0) {` ` ` `int` `cur = 0;` ` ` `// cur will be the sum of all` ` ` `// powers of 2 < k` ` ` `for` `(` `int` `i = 0; i < k; i++) {` ` ` `// If the current bit is set` ` ` `if` `(n & (1ll << i))` ` ` `cur += 1ll << i;` ` ` `}` ` ` `// Add Kth power of 2 to n and` ` ` `// subtract the all powers of 2` ` ` `// less than K that are set` ` ` `ans = n - cur + (1ll << k);` ` ` `}` ` ` `// If the kth bit is set` ` ` `else` `{` ` ` `int` `first_unset_bit = -1, cur = 0;` ` ` `for` `(` `int` `i = 0; i < 64; i++) {` ` ` `// First unset bit position` ` ` `if` `((n & (1ll << i)) == 0) {` ` ` `first_unset_bit = i;` ` ` `break` `;` ` ` `}` ` ` `// sum of bits that are set` ` ` `else` ` ` `cur += (1ll << i);` ` ` `}` ` ` `// Add Kth power of 2 to n and` ` ` `// subtract the all powers of 2` ` ` `// less than K that are set` ` ` `ans = n - cur` ` ` `+ (1ll << first_unset_bit);` ` ` `// If Kth bit became unset` ` ` `// then set it again` ` ` `if` `((ans & (1ll << k)) == 0)` ` ` `ans += (1ll << k);` ` ` `}` ` ` `// Return the resultant number` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 15, K = 2;` ` ` `// Print ans` ` ` `cout << find_next(N, K);` ` ` `return` `0;` `}` |

## Java

`// Java program for` `// the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the number` `// greater than n whose Kth` `// bit is set` `static` `int` `find_next(` `int` `n,` ` ` `int` `k)` `{` ` ` `// Stores the resultant` ` ` `// number` ` ` `int` `ans = ` `0` `;` ` ` `// If Kth bit is not set` ` ` `if` `((n & (1L << k)) == ` `0` `)` ` ` `{` ` ` `int` `cur = ` `0` `;` ` ` `// cur will be the sum of all` ` ` `// powers of 2 < k` ` ` `for` `(` `int` `i = ` `0` `; i < k; i++)` ` ` `{` ` ` `// If the current bit is set` ` ` `if` `((n & (1L << i)) > ` `0` `)` ` ` `cur += 1L << i;` ` ` `}` ` ` `// Add Kth power of 2 to n and` ` ` `// subtract the all powers of 2` ` ` `// less than K that are set` ` ` `ans = (` `int` `)(n - cur + (1L << k));` ` ` `}` ` ` `// If the kth bit is set` ` ` `else` ` ` `{` ` ` `int` `first_unset_bit = -` `1` `, cur = ` `0` `;` ` ` `for` `(` `int` `i = ` `0` `; i < ` `64` `; i++)` ` ` `{` ` ` `// First unset bit position` ` ` `if` `((n & (1L << i)) == ` `0` `)` ` ` `{` ` ` `first_unset_bit = i;` ` ` `break` `;` ` ` `}` ` ` `// sum of bits that are set` ` ` `else` ` ` `cur += (1L << i);` ` ` `}` ` ` `// Add Kth power of 2 to n and` ` ` `// subtract the all powers of 2` ` ` `// less than K that are set` ` ` `ans = (` `int` `)(n - cur +` ` ` `(1L << first_unset_bit));` ` ` `// If Kth bit became unset` ` ` `// then set it again` ` ` `if` `((ans & (1L << k)) == ` `0` `)` ` ` `ans += (1L << k);` ` ` `}` ` ` `// Return the resultant number` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `15` `, K = ` `2` `;` ` ` `// Print ans` ` ` `System.out.print(find_next(N, K));` `}` `}` `// This code is contributed by Rajput-Ji` |

## Python3

`# Python3 program for the above approach` `# Function to find the number greater` `# than n whose Kth bit is set` `def` `find_next(n, k):` ` ` `# Stores the resultant number` ` ` `ans ` `=` `0` ` ` ` ` `# If Kth bit is not set` ` ` `if` `((n & (` `1` `<< k)) ` `=` `=` `0` `):` ` ` `cur ` `=` `0` ` ` `# cur will be the sum of all` ` ` `# powers of 2 < k` ` ` `for` `i ` `in` `range` `(k):` ` ` `# If the current bit is set` ` ` `if` `(n & (` `1` `<< i)):` ` ` `cur ` `+` `=` `1` `<< i` ` ` `# Add Kth power of 2 to n and` ` ` `# subtract the all powers of 2` ` ` `# less than K that are set` ` ` `ans ` `=` `n ` `-` `cur ` `+` `(` `1` `<< k)` ` ` `# If the kth bit is set` ` ` `else` `:` ` ` `first_unset_bit, cur ` `=` `-` `1` `, ` `0` ` ` `for` `i ` `in` `range` `(` `64` `):` ` ` `# First unset bit position` ` ` `if` `((n & (` `1` `<< i)) ` `=` `=` `0` `):` ` ` `first_unset_bit ` `=` `i` ` ` `break` ` ` `# Sum of bits that are set` ` ` `else` `:` ` ` `cur ` `+` `=` `(` `1` `<< i)` ` ` `# Add Kth power of 2 to n and` ` ` `# subtract the all powers of 2` ` ` `# less than K that are set` ` ` `ans ` `=` `n ` `-` `cur ` `+` `(` `1` `<< first_unset_bit)` ` ` `# If Kth bit became unset` ` ` `# then set it again` ` ` `if` `((ans & (` `1` `<< k)) ` `=` `=` `0` `):` ` ` `ans ` `+` `=` `(` `1` `<< k)` ` ` `# Return the resultant number` ` ` `return` `ans` ` ` `# Driver code` `N, K ` `=` `15` `, ` `2` `# Print ans` `print` `(find_next(N, K))` `# This code is contributed by divyeshrabadiya07` |

## C#

`// C# program for` `// the above approach` `using` `System;` `class` `GFG{` `// Function to find the number` `// greater than n whose Kth` `// bit is set` `static` `int` `find_next(` `int` `n,` ` ` `int` `k)` `{` ` ` `// Stores the resultant` ` ` `// number` ` ` `int` `ans = 0;` ` ` `// If Kth bit is not set` ` ` `if` `((n & (1L << k)) == 0)` ` ` `{` ` ` `int` `cur = 0;` ` ` `// cur will be the sum of all` ` ` `// powers of 2 < k` ` ` `for` `(` `int` `i = 0; i < k; i++)` ` ` `{` ` ` `// If the current bit is set` ` ` `if` `((n & (1L << i)) > 0)` ` ` `cur += (` `int` `)1L << i;` ` ` `}` ` ` `// Add Kth power of 2 to n and` ` ` `// subtract the all powers of 2` ` ` `// less than K that are set` ` ` `ans = (` `int` `)(n - cur + (1L << k));` ` ` `}` ` ` `// If the kth bit is set` ` ` `else` ` ` `{` ` ` `int` `first_unset_bit = -1, cur = 0;` ` ` `for` `(` `int` `i = 0; i < 64; i++)` ` ` `{` ` ` `// First unset bit position` ` ` `if` `((n & (1L << i)) == 0)` ` ` `{` ` ` `first_unset_bit = i;` ` ` `break` `;` ` ` `}` ` ` `// Sum of bits that are set` ` ` `else` ` ` `cur +=(` `int` `)(1L << i);` ` ` `}` ` ` `// Add Kth power of 2 to n and` ` ` `// subtract the all powers of 2` ` ` `// less than K that are set` ` ` `ans = (` `int` `)(n - cur +` ` ` `(1L << first_unset_bit));` ` ` `// If Kth bit became unset` ` ` `// then set it again` ` ` `if` `((ans & (1L << k)) == 0)` ` ` `ans += (` `int` `)(1L << k);` ` ` `}` ` ` `// Return the resultant number` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `N = 15, K = 2;` ` ` `// Print ans` ` ` `Console.Write(find_next(N, K));` `}` `}` `// This code is contributed by Rajput-Ji` |

**Output**

20

**Time Complexity:** O(K)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**