Minimize OR of Remaining Elements Using Operations

You are given a 0-indexed integer array nums and an integer k.

In one operation, you can pick any index i of nums such that 0 <= i < nums.length - 1 and replace nums[i] and nums[i + 1] with a single occurrence of nums[i] & nums[i + 1], where & represents the bitwise AND operator.

Return the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.

Example 1
Inputnums = [3,5,3,2,7], k = 2
Output3
After replacing adjacent pairs to get [1, 3, 2], the bitwise OR is 3, which is the minimum possible value.
Example 2
Inputnums = [7,3,15,14,2,8], k = 4
Output2
After four operations, the array can become [2, 0], whose bitwise OR is 2, which is the minimum possible value.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] < 2^30
  • 0 <= k < nums.length

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate