Minimum Operations to Make Array Elements Zero - Problem
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.
In one operation, you can:
- Select two integers
aandbfrom the array. - Replace them with
floor(a / 4)andfloor(b / 4).
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.
Input & Output
Example 1 — Basic Range
$
Input:
queries = [[1,3]]
›
Output:
3
💡 Note:
Range [1,3]: 1 needs 1 op (1→0), 2 needs 1 op (2→0), 3 needs 1 op (3→0). Total: 1+1+1 = 3.
Example 2 — Larger Numbers
$
Input:
queries = [[5,7]]
›
Output:
6
💡 Note:
Range [5,7]: 5→1→0 (2 ops), 6→1→0 (2 ops), 7→1→0 (2 ops). Total: 2+2+2 = 6
Example 3 — Multiple Queries
$
Input:
queries = [[1,2],[3,4]]
›
Output:
5
💡 Note:
Query [1,2]: 1→0 (1 op), 2→0 (1 op). Sum=2. Query [3,4]: 3→0 (1 op), 4→1→0 (2 ops). Sum=3. Total: 2+3 = 5.
Constraints
- 1 ≤ queries.length ≤ 105
- 1 ≤ l ≤ r ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code