Problem
You are given two integers n and x. You have to construct an array of positive integers nums of size n where for every 0 <= i < n - 1, nums[i + 1] is greater than nums[i], and the result of the bitwise AND operation between all elements of nums is x.
Return the minimum possible value of nums[n - 1].
Examples
Example 1:
| |
Example 2:
| |
Solution
Method 1 - Bit manipulation using OR
The smallest element equals x, and all elements look like x with some additional bits. So, initially, num can be equal to x, and then each time we increment num by 1, and apply bitwise OR with x, we will get next valid bigger element.
And we have to repeat above operation n - 1 times (as x is already the first element) , to get our ans.
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(n), as the loop runsn - 1times - 🧺 Space complexity:
O(1), as we only use a few extra variables.
Method 2 - Bit interweaving
The key idea is to utilize the bits of n-1 and strategically place them into the positions where x has zero bits. Lets take number 22 = 10110, and let n = 4.
As x is already the first number, we just need n - 1 numbers, i.e. 3.
So, we can decrement from n to 1 like in previous approach, and interweave it around the existing bits in x.
The elements, smallest to largest, starting from x, will look like:
- 10110
- 10111
- 11110
- 11111
- .. and so on
$$ x_0 = \begin{array}{|c|c|c|c|c|} \hline \colorbox{lightgreen}{1} & {0} & \colorbox{lightgreen}{1} & \colorbox{lightgreen}{1} & 0 \\ \hline \end{array} $$ $$ x_1 = \begin{array}{|c|c|c|c|c|} \hline \colorbox{lightgreen}{1} & {0} & \colorbox{lightgreen}{1} & \colorbox{lightgreen}{1} & \colorbox{orange}{1} \\ \hline \end{array} $$ $$ x_2 = \begin{array}{|c|c|c|c|c|} \hline \colorbox{lightgreen}{1} & \colorbox{orange}{1} & \colorbox{lightgreen}{1} & \colorbox{lightgreen}{1} & 0 \\ \hline \end{array} $$ $$ x_3 = \begin{array}{|c|c|c|c|c|} \hline \colorbox{lightgreen}{1} & \colorbox{orange}{1} & \colorbox{lightgreen}{1} & \colorbox{lightgreen}{1} & \colorbox{orange}{1} \\ \hline \end{array} $$ And so on….
Approach
- Initialization:
- We decrement
nby 1 (n = n - 1) becausexis already considered as the first element of our array, so we only need to find the othern-1elements.
- We decrement
- Bit Manipulation:
- We use a bit variable
bwhich is initially set to 1 and keep shifting it left (b <<= 1) in each iteration to explore each bit position.
- We use a bit variable
- Decision Making:
- For each bit position
b, we check if it is an empty bit inxby comparingb & xwith 0.- If
(b & x) == 0(i.e., the bit is not set inx), we have two choices:- Leave it empty.
- Fill it.
- By iterating through all possible empty bits, we can find the smallest possible number that satisfies the condition.
- If
- For each bit position
- Setting Bits:
- If the first bit of
n(i.e.,n & 1) is on, we turn on the corresponding bit inabya |= (n & 1) * b. - Right-shift
nby 1 (n >>= 1) to process the next bit.
- If the first bit of
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(70)- Even with a large value for
n, the number of necessary iterations is limited by the bits in the final number. - For example,
10^8 ≈ 2^26implies2^26as the starting value forx. - With
nset to10^8, the maximum bits in the final number cannot exceed26 + 27 = 53.
- Even with a large value for
- 🧺 Space complexity:
O(1)