Next greater integer having one more number of set bits
Last Updated : 30 May, 2021
Given a positive integer 'n' having 'x' number of set bits in its binary representation. The problem is to find the next greater integer(smallest integer greater than n), having (x+1) number of set bits in its binary representation.
Examples :
Input : 10 Output : 11 (10)10 = (1010)2 is having 2 set bits. (11)10 = (1011)2 is having 3 set bits and is the next greater. Input : 39 Output : 47
Approach: Following are the steps:
- Find the position of the rightmost unset bit(considering last bit at position 0, second last bit at position 1 and so on) in the binary representation of n.
- Let the position be represented by pos.
- Set the bit at position pos. Refer this post.
- If there are no unset bits in the binary representation, then perform bitwise left shift by 1 on the given number and then add 1 to it.
How to get the position of rightmost unset bit?
- Perform bitwise not on the given number(operation equivalent to 1's complement).Let it be num = ~n.
- Get the position of rightmost set bit of num.
C++ // C++ implementation to find the next greater integer // with one more number of set bits #include <bits/stdc++.h> using namespace std; // function to find the position of rightmost // set bit. Returns -1 if there are no set bits int getFirstSetBitPos(int n) { return (log2(n&-n)+1) - 1; } // function to find the next greater integer int nextGreaterWithOneMoreSetBit(int n) { // position of rightmost unset bit of n // by passing ~n as argument int pos = getFirstSetBitPos(~n); // if n consists of unset bits, then // set the rightmost unset bit if (pos > -1) return (1 << pos) | n; //n does not consists of unset bits return ((n << 1) + 1); } // Driver program to test above int main() { int n = 10; cout << "Next greater integer = " << nextGreaterWithOneMoreSetBit(n); return 0; }
Java // Java implementation to find the next greater integer // with one more number of set bits class GFG { // function to find the position of rightmost // set bit. Returns -1 if there are no set bits static int getFirstSetBitPos(int n) { return ((int)(Math.log(n & -n) / Math.log(2)) + 1) - 1; } // function to find the next greater integer static int nextGreaterWithOneMoreSetBit(int n) { // position of rightmost unset bit of n // by passing ~n as argument int pos = getFirstSetBitPos(~n); // if n consists of unset bits, then // set the rightmost unset bit if (pos > -1) return (1 << pos) | n; // n does not consists of unset bits return ((n << 1) + 1); } // Driver code public static void main(String[] args) { int n = 10; System.out.print("Next greater integer = " + nextGreaterWithOneMoreSetBit(n)); } } // This code is contributed by Anant Agarwal.
Python3 # Python3 implementation to find # the next greater integer with # one more number of set bits import math # Function to find the position # of rightmost set bit. Returns -1 # if there are no set bits def getFirstSetBitPos(n): return ((int)(math.log(n & -n) / math.log(2)) + 1) - 1 # Function to find the next greater integer def nextGreaterWithOneMoreSetBit(n): # position of rightmost unset bit of # n by passing ~n as argument pos = getFirstSetBitPos(~n) # if n consists of unset bits, then # set the rightmost unset bit if (pos > -1): return (1 << pos) | n # n does not consists of unset bits return ((n << 1) + 1) # Driver code n = 10 print("Next greater integer = ", nextGreaterWithOneMoreSetBit(n)) # This code is contributed by Anant Agarwal.
C# // C# implementation to find the next greater // integer with one more number of set bits using System; class GFG { // function to find the position of rightmost // set bit. Returns -1 if there are no set bits static int getFirstSetBitPos(int n) { return ((int)(Math.Log(n & -n) / Math.Log(2)) + 1) - 1; } // function to find the next greater integer static int nextGreaterWithOneMoreSetBit(int n) { // position of rightmost unset bit of n // by passing ~n as argument int pos = getFirstSetBitPos(~n); // if n consists of unset bits, then // set the rightmost unset bit if (pos > -1) return (1 << pos) | n; // n does not consists of unset bits return ((n << 1) + 1); } // Driver code public static void Main() { int n = 10; Console.Write("Next greater integer = " + nextGreaterWithOneMoreSetBit(n)); } } // This code is contributed by Anant Agarwal.
PHP <?php // PHP implementation to find the // next greater integer with // one more number of set bits // function to find the position // of rightmost set bit. Returns // -1 if there are no set bits function getFirstSetBitPos($n) { return (log($n & -$n + 1)) - 1; } // function to find the // next greater integer function nextGreaterWithOneMoreSetBit($n) { // position of rightmost unset bit of n // by passing ~n as argument $pos = getFirstSetBitPos(~$n); // if n consists of unset bits, then // set the rightmost unset bit if ($pos > -1) return (1 << $pos) | $n; //n does not consists of unset bits return (($n << 1) + 1); } // Driver Code $n = 10; echo "Next greater integer = ", nextGreaterWithOneMoreSetBit($n); // This code is contributed by Ajit ?>
JavaScript <script> // Javascript implementation to find the next greater integer // with one more number of set bits // function to find the position of rightmost // set bit. Returns -1 if there are no set bits function getFirstSetBitPos(n) { return (parseInt(Math.log(n&-n)/Math.log(2))+1) - 1; } // function to find the next greater integer function nextGreaterWithOneMoreSetBit(n) { // position of rightmost unset bit of n // by passing ~n as argument var pos = getFirstSetBitPos(~n); // if n consists of unset bits, then // set the rightmost unset bit if (pos > -1) return (1 << pos) | n; //n does not consists of unset bits return ((n << 1) + 1); } // Driver program to test above var n = 10; document.write("Next greater integer = " + nextGreaterWithOneMoreSetBit(n)); </script>
Output :
Next greater integer = 11
Time Complexity: O(log(log N))
Auxiliary Space: O(1)
Similar Reads
Previous smaller integer having one less number of set bits Given a positive integer ânâ having âxâ number of set bits in its binary representation. The problem is to find the previous smaller integer(greatest integer smaller than n), having (x-1) number of set bits in its binary representation.Note: 1 <= n Examples : Input : 8 Output : 0 (8)10 = (1000)2
4 min read
M-th smallest number having k number of set bits. Given two non-negative integers m and k. The problem is to find the m-th smallest number having k number of set bits.Constraints: 1 <= m, k.Examples: Input : m = 4, k = 2 Output : 9 (9)10 = (1001)2, it is the 4th smallest number having 2 set bits. Input : m = 6, k = 4 Output : 39 Approach: Follow
6 min read
Number of integers with odd number of set bits Given a number n, count number of integers smaller than or equal to n that have odd number of set bits.Examples: Input : 5 Output : 3 Explanation : Integers with odd number of set bits in range 1 to 5 : 0 contains 0 set bits 1 contains 1 set bits 2 contains 1 set bits 3 contains 2 set bits 4 contain
6 min read
Count of numbers having only one unset bit in a range [L,R] Given two integers L and R, the task is to count the numbers having only one unset bit in the range [L, R]. Examples: Input: L = 4, R = 9Output: 2Explanation:The binary representation of all numbers in the range [4, 9] are 4 = (100)2 5 = (101)2 6 = (110)2 7 = (111)2 8 = (1000)2 9 = (1001)2 Out of al
11 min read
Next higher number with same number of set bits Given a number x, find next number with same number of 1 bits in it's binary representation.For example, consider x = 12, whose binary representation is 1100 (excluding leading zeros on 32 bit machine). It contains two logic 1 bits. The next higher number with two logic 1 bits is 17 (100012).Algorit
8 min read
Largest number less than equal to N having exactly K set bits Given two positive integers N and K, we need to determine if there exists a positive integer X less than or equal to N such that the number of 1s in the binary representation of X is equal to k (i.e., f(X) = K). If such an X exists, we also need to find the maximum value of X and print -1 if there i
13 min read