3022. Minimize OR of Remaining Elements Using Operations Hard
1/**
2 * [3022] Minimize OR of Remaining Elements Using Operations
3 *
4 * You are given a 0-indexed integer array nums and an integer k.
5 * 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.
6 * Return the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
7 *
8 * <strong class="example">Example 1:
9 *
10 * Input: nums = [3,5,3,2,7], k = 2
11 * Output: 3
12 * Explanation: Let's do the following operations:
13 * 1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [1,3,2,7].
14 * 2. Replace nums[2] and nums[3] with (nums[2] & nums[3]) so that nums becomes equal to [1,3,2].
15 * The bitwise-or of the final array is 3.
16 * 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.
17 * <strong class="example">Example 2:
18 *
19 * Input: nums = [7,3,15,14,2,8], k = 4
20 * Output: 2
21 * Explanation: Let's do the following operations:
22 * 1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,15,14,2,8].
23 * 2. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,14,2,8].
24 * 3. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [2,2,8].
25 * 4. Replace nums[1] and nums[2] with (nums[1] & nums[2]) so that nums becomes equal to [2,0].
26 * The bitwise-or of the final array is 2.
27 * 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.
28 *
29 * <strong class="example">Example 3:
30 *
31 * Input: nums = [10,7,10,3,9,14,9,4], k = 1
32 * Output: 15
33 * Explanation: Without applying any operations, the bitwise-or of nums is 15.
34 * 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.
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= nums.length <= 10^5
40 * 0 <= nums[i] < 2^30
41 * 0 <= k < nums.length
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimize-or-of-remaining-elements-using-operations/
47// discuss: https://leetcode.com/problems/minimize-or-of-remaining-elements-using-operations/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn min_or_after_operations(nums: Vec<i32>, k: i32) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_3022() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.