Minimum Array End - Problem

You're tasked with constructing a strictly increasing array of positive integers with a special bitwise property. Given two integers n and x, you need to build an array nums of size n where:

  • Each element is strictly greater than the previous one (nums[i+1] > nums[i])
  • The bitwise AND of all elements equals x

Your goal is to find the minimum possible value of the last element nums[n-1]. This is a fascinating bit manipulation challenge that requires understanding how AND operations preserve certain bits across multiple numbers.

Example: If n = 3 and x = 4, you need 3 numbers where a & b & c = 4. Since 4 = 100โ‚‚, all numbers must have bit 2 set. The optimal array might be [4, 5, 6] giving us the minimum last element of 6.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 3, x = 4
โ€บ Output: 6
๐Ÿ’ก Note: We need 3 numbers where their AND equals 4 (binary: 100). Since bit 2 must be set in all numbers, the minimum array could be [4, 5, 6]. The bitwise AND is 4 & 5 & 6 = 4, and the last element is 6.
example_2.py โ€” Single Element
$ Input: n = 1, x = 5
โ€บ Output: 5
๐Ÿ’ก Note: When n=1, we only need one number, and it must be at least x to satisfy the AND condition. So the array is [5] and the last element is 5.
example_3.py โ€” Larger Sequence
$ Input: n = 6, x = 5
โ€บ Output: 13
๐Ÿ’ก Note: We need 6 numbers where AND equals 5 (binary: 101). The optimal array is [5, 7, 9, 11, 13]. All preserve bits 0 and 2 from x=5, giving AND result of 5.

Constraints

  • 1 โ‰ค n โ‰ค 108
  • 1 โ‰ค x โ‰ค 108
  • All array elements must be positive integers
  • The array must be strictly increasing

Visualization

Tap to expand
Binary Combination Lock AnalogyMaster Lock Pattern (x = 5 = 101โ‚‚)101FIXEDFREEFIXEDLock Sequence (n = 6)Each lock must preserve fixed switches but can vary free switchesLock 1: 101โ‚‚Lock 2: 111โ‚‚Lock 3: 1001โ‚‚Lock 4: 1011โ‚‚Lock 5: 1101โ‚‚Lock 6: 1111โ‚‚Bit Manipulation Magic1. Find free bit positions (where x has 0s)2. Convert (n-1) to binary: 5 = 101โ‚‚3. Place these bits in free positions: x | (bits in free slots)Result: 1101โ‚‚ = 13๐Ÿ’ก Key Insight: We only need to calculate the last element directly!Time: O(log x) | Space: O(1) - No need to build the entire array
Understanding the Visualization
1
Master Lock Pattern
The master lock (x) shows which switches must be ON in every lock
2
Available Switches
Switches that are OFF in the master can be freely toggled
3
Counting Pattern
Use these free switches to create an increasing binary count
4
Minimum Result
This gives us the smallest possible nth lock combination
Key Takeaway
๐ŸŽฏ Key Insight: By understanding that we can only modify '0' bit positions in x and treating them as binary counter slots, we can directly compute the minimum array end in O(log x) time without constructing the entire array.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
38.0K Views
Medium Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen