‘n = 6’
The binary representation of 6 = ‘0110’, which has two ‘1’ bits set.
Next smallest integer = 9 = ‘1001’, the smallest integer greater than 6 having two ‘1’ bits set.
Previous largest integer = 5 = ‘0101’, largest integer smaller than 6 having two ‘1’ bits set.
Therefore, the answer is {9, 5}.
1. ‘n’ is a positive integer greater than one.
2. For any given integer ‘n’, there is always a positive integer smaller than ‘n’ having the exact number of ‘1’ bits set.
3. Don’t print anything. Just implement the function and return the answer.
4. ‘n’ can have a max of ‘30’ bits.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next line of each test case consists of a single line containing the integer ‘n’.
For each test case, print the next smallest integer, the previous largest integer to ‘n’ with the exact number of ‘1’ bits set.
The output of each test case should be printed in a new line.
1 <= T <= 10000
‘n’ can have ‘30’ bits at max
Where ‘T’ is the number of test cases, and ‘n’ is the given integer.
Time limit: 1 second
The brute force approach would be to count the number of ‘1’ bits in ‘n’ and then increment (or decrement) from ‘n’ until we find an integer with an exact count of ‘1’ bits.
Observation: Consider the location of two bits for a number ‘n’ as ‘i’ and ‘j’. To maintain the same number of 1 bit in ‘n’, if we flip the ‘i-th’ bit from 1 to 0, we must convert the ‘j-th’ bit from 0 to 1. If ‘i’ is to the left of ‘j’ then the number will increase. Otherwise, it will decrease.
1. To find the next smallest number:
Let 'next' be the next smallest number to ‘n’ having the same count of ‘1’ bits as ‘n’. To obtain 'next' from ‘n’ we observe:
Steps to obtain 'next':
2. To find the previous largest number:
Let 'prev' be the previous largest number to ‘n’ having the same count of ‘1’ bits as ‘n’. we follow a similar approach as above. Instead of flipping the rightmost non-trailing zero, the only difference is that we flip the rightmost non-trailing one because here, we want 'prev' to be smaller than ‘n’.
Steps to obtain 'prev':
This approach is similar to the bit-manipulation approach, but we want to use as little bit-manipulation as possible.
1. To find the next smallest number:
Let 'next' be the next smallest number to ‘n’ having the same count of ‘1’ bits as ‘n’.
If ‘r’ is the position of the rightmost non-trailing zero in ‘n’, ‘c1’ is the number of 1’s to the right of ‘r’, and ‘c0’ is the number of trailing 0’s in ‘n’, we have ‘r = c0 + c1’.
After initializing ‘next = n’, in our previous approach we performed the following operations on ‘next’:
2. To find the previous largest number:
Let ‘prev’ be the previous largest number to ‘n’ having the same count of ‘1’ bits as ‘n’.
If ‘r’ is the position of the rightmost non-trailing one in ‘n’, ‘c0’ is the number of 0’s to the right of ‘r’, and ‘c1’ is the number of trailing 1’s in ‘n’, we have ‘r = c0 + c1’.
After initializing ‘prev = n’, in our previous approach, we performed the following operations on ‘prev’:
Note: We can calculate ‘2^x’ efficiently using the left-shift (‘<<’) operator.
‘2^x = 1<<x’ (Here, ‘x >= 0’ and ‘2^x’ is in the valid integer range)
Detect Odd
Detect Odd
Ninja And The Clan
Check whether K-th bit is set or not
Check whether K-th bit is set or not
Maximum Element
XOR DARE