Minimum Array End - 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]
  • The result of the bitwise AND operation between all elements of nums is x

Return the minimum possible value of nums[n - 1].

Input & Output

Example 1 — Basic Case
$ Input: n = 3, x = 4
Output: 6
💡 Note: Array [4,5,6] has AND = 4 & 5 & 6 = 4, strictly increasing, minimum last element is 6
Example 2 — Single Element
$ Input: n = 1, x = 2
Output: 2
💡 Note: Array [2] has AND = 2, only one element so result is 2
Example 3 — Larger Case
$ Input: n = 4, x = 1
Output: 7
💡 Note: Array [1,3,5,7] has AND = 1 & 3 & 5 & 7 = 1, strictly increasing, minimum last element is 7

Constraints

  • 1 ≤ n ≤ 108
  • 1 ≤ x ≤ 108

Visualization

Tap to expand
Minimum Array End: Build array where AND = xInput: n=3, x=4Need 3 numsAND must = 4Constraints• Strictly increasing• AND of all = x• Minimize last elementSolution Process456Array: [4, 5, 6]Verification4 AND 5 AND 6 = 4 ✓4 < 5 < 6 ✓Last element = 6 (minimum possible)Output: 6
Understanding the Visualization
1
Input
n=3 numbers needed, AND result must be x=4
2
Constraint
Array must be strictly increasing and AND = x
3
Output
Minimum possible last element
Key Takeaway
🎯 Key Insight: Use x as a bit template and fill zero positions with binary patterns to create minimal increasing sequence
Asked in
Microsoft 25 Google 20
28.0K Views
Medium Frequency
~25 min Avg. Time
890 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