3495. Minimum Operations to Make Array Elements Zero Hard
1/**
2 * [3495] Minimum Operations to Make Array Elements Zero
3 *
4 * You are given a 2D array queries, where queries[i] is of the form [l, r]. Each queries[i] defines an array of integers nums consisting of elements ranging from l to r, both inclusive.
5 * In one operation, you can:
6 *
7 * Select two integers a and b from the array.
8 * Replace them with floor(a / 4) and floor(b / 4).
9 *
10 * Your task is to determine the minimum number of operations required to reduce all elements of the array to zero for each query. Return the sum of the results for all queries.
11 *
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">queries = [[1,2],[2,4]]</span>
15 * Output: <span class="example-io">3</span>
16 * Explanation:
17 * For queries[0]:
18 *
19 * The initial array is nums = [1, 2].
20 * In the first operation, select nums[0] and nums[1]. The array becomes [0, 0].
21 * The minimum number of operations required is 1.
22 *
23 * For queries[1]:
24 *
25 * The initial array is nums = [2, 3, 4].
26 * In the first operation, select nums[0] and nums[2]. The array becomes [0, 3, 1].
27 * In the second operation, select nums[1] and nums[2]. The array becomes [0, 0, 0].
28 * The minimum number of operations required is 2.
29 *
30 * The output is 1 + 2 = 3.
31 * </div>
32 * <strong class="example">Example 2:
33 * <div class="example-block">
34 * Input: <span class="example-io">queries = [[2,6]]</span>
35 * Output: <span class="example-io">4</span>
36 * Explanation:
37 * For queries[0]:
38 *
39 * The initial array is nums = [2, 3, 4, 5, 6].
40 * In the first operation, select nums[0] and nums[3]. The array becomes [0, 3, 4, 1, 6].
41 * In the second operation, select nums[2] and nums[4]. The array becomes [0, 3, 1, 1, 1].
42 * In the third operation, select nums[1] and nums[2]. The array becomes [0, 0, 0, 1, 1].
43 * In the fourth operation, select nums[3] and nums[4]. The array becomes [0, 0, 0, 0, 0].
44 * The minimum number of operations required is 4.
45 *
46 * The output is 4.
47 * </div>
48 *
49 * Constraints:
50 *
51 * 1 <= queries.length <= 10^5
52 * queries[i].length == 2
53 * queries[i] == [l, r]
54 * 1 <= l < r <= 10^9
55 *
56 */
57pub struct Solution {}
58
59// problem: https://leetcode.com/problems/minimum-operations-to-make-array-elements-zero/
60// discuss: https://leetcode.com/problems/minimum-operations-to-make-array-elements-zero/discuss/?currentPage=1&orderBy=most_votes&query=
61
62// submission codes start here
63
64impl Solution {
65 pub fn min_operations(queries: Vec<Vec<i32>>) -> i64 {
66
67 }
68}
69
70// submission codes end
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn test_3495() {
78 }
79}
80Back
© 2026 bowen.ge All Rights Reserved.