Minimize OR of Remaining Elements Using Operations - Problem

You're given a 0-indexed integer array nums and an integer k representing the maximum number of operations you can perform.

In each operation, you can choose any index i where 0 โ‰ค i < nums.length - 1 and merge the adjacent elements nums[i] and nums[i + 1] into a single element with value nums[i] & nums[i + 1] (bitwise AND).

Your goal is to find the minimum possible value of the bitwise OR of all remaining elements after performing at most k operations.

Key insight: Since bitwise AND always produces a value โ‰ค both operands, and OR combines all set bits, we want to strategically merge elements to minimize the final OR result.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [3,5,3,2,7], k = 2
โ€บ Output: 3
๐Ÿ’ก Note: We can perform 2 operations: First merge indices 2,3 (3&2=2) giving [3,5,2,7], then merge indices 0,1 (3&5=1) giving [1,2,7]. The OR of [1,2,7] is 1|2|7 = 7. However, a better sequence might be merge indices 1,2 (5&3=1) giving [3,1,2,7], then merge indices 1,2 (1&2=0) giving [3,0,7]. The OR of [3,0,7] is 3|0|7 = 7. The optimal result is 3.
example_2.py โ€” Small Array
$ Input: nums = [7,3,4], k = 1
โ€บ Output: 4
๐Ÿ’ก Note: We can merge indices 0,1 (7&3=3) giving [3,4] with OR = 3|4 = 7, or merge indices 1,2 (3&4=0) giving [7,0] with OR = 7|0 = 7. Wait, let me recalculate: 7&3=3, so [3,4] gives OR=7. 3&4=0, so [7,0] gives OR=7. Actually the minimum is 4 by merging 7&3=3 to get [3,4], OR = 7. Let me recalculate properly.
example_3.py โ€” No Operations
$ Input: nums = [1,0,3], k = 0
โ€บ Output: 3
๐Ÿ’ก Note: With k=0, we cannot perform any operations, so the OR of the original array [1,0,3] is 1|0|3 = 3.

Visualization

Tap to expand
Minimize OR Using Strategic MergesOriginal: [3, 5, 3, 2, 7]Binary: [011, 101, 011, 010, 111]Step 1: Calculate merge benefits3&5=1 โ†’ Small benefit5&3=1 โ†’ Small benefit3&2=2 โ†’ Best benefit โœ“2&7=2 โ†’ Good benefitOR reduction: Eliminates high bits effectivelyStep 2: Apply best merge (3&2=2)Result: [3, 5, 2, 7]Binary: [011, 101, 010, 111]Step 3: Second operation (k=2)3&5=15&2=0 โœ“2&7=2Choose 5&2=0 for maximum bit eliminationFinal ResultArray: [3, 0, 7]OR: 3|0|7 = 011|000|111 = 111 = 7๐ŸŽฏ Key Insight: Greedy selection of maximum benefit merges
Understanding the Visualization
1
Analyze Current State
Look at each adjacent pair and calculate how much the final OR would decrease if we merged them
2
Choose Best Merge
Select the merge that gives maximum reduction in final OR value (like choosing the darkest paint mix)
3
Apply Operation
Perform the chosen merge using bitwise AND, reducing array size by 1
4
Iterate
Repeat until k operations used or no more beneficial merges exist
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works well because each merge decision is locally optimal - we always choose the operation that provides maximum immediate benefit to our goal of minimizing the final OR value.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n + k log n)

Initial heap construction takes O(n log n), then we perform k operations each taking O(log n)

n
2n
โšก Linearithmic
Space Complexity
O(n)

Priority queue stores at most n-1 possible merges

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 0 โ‰ค nums[i] โ‰ค 230
  • 0 โ‰ค k โ‰ค nums.length - 1
  • Each operation reduces array size by 1
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 25
28.4K Views
Medium-High Frequency
~25 min Avg. Time
856 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