You are given a 0-indexed integer array nums of length n and an integer k. In an operation, you can choose an element and multiply it by 2.

Return the maximum possible value of nums[0] | nums[1] | ... | nums[n - 1] that can be obtained after applying the operation on nums at most k times.

Note that a | b denotes the bitwise OR between two integers a and b.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,2,1], k = 2
Output: 15
💡 Note: We can apply operations on nums[0] twice: 3 → 6 → 12. Then nums becomes [12,2,1] and the OR is 12|2|1 = 15.
Example 2 — Single Element
$ Input: nums = [2], k = 3
Output: 16
💡 Note: With only one element, apply all operations: 2 → 4 → 8 → 16. The OR is just 16.
Example 3 — Zero Operations
$ Input: nums = [7,3,5], k = 0
Output: 7
💡 Note: No operations allowed, so OR is 7|3|5 = 7.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ 15

Visualization

Tap to expand
Maximum OR Problem: Enhance Array Elements321Input: k=2 operationsApply k operations to maximize OR12213×2×2Maximum OR = 12|2|1 = 15Concentrate operations on one element for maximum bit impact
Understanding the Visualization
1
Input
Array [3,2,1] with k=2 operations available
2
Apply Operations
Test applying all operations to each element
3
Calculate OR
Find which choice gives maximum OR value
Key Takeaway
🎯 Key Insight: Apply all k operations to a single element to maximize the highest bit position in the OR result
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
23.4K Views
Medium Frequency
~25 min Avg. Time
892 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