2680. Maximum OR Medium

@problem@discussion
#Array#Greedy#Bit Manipulation#Prefix Sum



1/**
2 * [2680] Maximum OR
3 *
4 * 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.
5 * 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.
6 * Note that a | b denotes the bitwise or between two integers a and b.
7 *  
8 * <strong class="example">Example 1:
9 * 
10 * Input: nums = [12,9], k = 1
11 * Output: 30
12 * Explanation: If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
13 * 
14 * <strong class="example">Example 2:
15 * 
16 * Input: nums = [8,1,2], k = 2
17 * Output: 35
18 * Explanation: If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.
19 * 
20 *  
21 * Constraints:
22 * 
23 * 	1 <= nums.length <= 10^5
24 * 	1 <= nums[i] <= 10^9
25 * 	1 <= k <= 15
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/maximum-or/
31// discuss: https://leetcode.com/problems/maximum-or/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn maximum_or(nums: Vec<i32>, k: i32) -> i64 {
37        
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_2680() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.