Class Solution
-
- All Implemented Interfaces:
public final class Solution3022 - Minimize OR of Remaining Elements Using Operations.
Hard
You are given a 0-indexed integer array
numsand an integerk.In one operation, you can pick any index
iofnumssuch that0 <= i < nums.length - 1and replacenums[i]andnums[i + 1]with a single occurrence ofnums[i] & nums[i + 1], where&represents the bitwiseANDoperator.Return the minimum possible value of the bitwise
ORof the remaining elements ofnumsafter applying at mostkoperations.Example 1:
Input: nums = 3,5,3,2,7, k = 2
Output: 3
Explanation: Let's do the following operations:
Replace nums0 and nums1 with (nums0& nums1) so that nums becomes equal to 1,3,2,7.
Replace nums2 and nums3 with (nums2& nums3) so that nums becomes equal to 1,3,2.
The bitwise-or of the final array is 3.
It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
Example 2:
Input: nums = 7,3,15,14,2,8, k = 4
Output: 2
Explanation: Let's do the following operations:
Replace nums0 and nums1 with (nums0& nums1) so that nums becomes equal to 3,15,14,2,8.
Replace nums0 and nums1 with (nums0& nums1) so that nums becomes equal to 3,14,2,8.
Replace nums0 and nums1 with (nums0& nums1) so that nums becomes equal to 2,2,8.
Replace nums1 and nums2 with (nums1& nums2) so that nums becomes equal to 2,0.
The bitwise-or of the final array is 2.
It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
Example 3:
Input: nums = 10,7,10,3,9,14,9,4, k = 1
Output: 15
Explanation: Without applying any operations, the bitwise-or of nums is 15. It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>0 <= numsi< 2<sup>30</sup></code>
0 <= k < nums.length
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminOrAfterOperations(IntArray nums, Integer k)-
-
Method Detail
-
minOrAfterOperations
final Integer minOrAfterOperations(IntArray nums, Integer k)
-
-
-
-