Class Solution
-
- All Implemented Interfaces:
public final class Solution3495 - Minimum Operations to Make Array Elements Zero.
Hard
You are given a 2D array
queries, wherequeries[i]is of the form[l, r]. Eachqueries[i]defines an array of integersnumsconsisting of elements ranging fromltor, 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.
Example 1:
Input: queries = [1,2,2,4]
Output: 3
Explanation:
For
queries[0]:The initial array is
nums = [1, 2].In the first operation, select
nums[0]andnums[1]. The array becomes[0, 0].The minimum number of operations required is 1.
For
queries[1]:The initial array is
nums = [2, 3, 4].In the first operation, select
nums[0]andnums[2]. The array becomes[0, 3, 1].In the second operation, select
nums[1]andnums[2]. The array becomes[0, 0, 0].The minimum number of operations required is 2.
The output is
1 + 2 = 3.Example 2:
Input: queries = [2,6]
Output: 4
Explanation:
For
queries[0]:The initial array is
nums = [2, 3, 4, 5, 6].In the first operation, select
nums[0]andnums[3]. The array becomes[0, 3, 4, 1, 6].In the second operation, select
nums[2]andnums[4]. The array becomes[0, 3, 1, 1, 1].In the third operation, select
nums[1]andnums[2]. The array becomes[0, 0, 0, 1, 1].In the fourth operation, select
nums[3]andnums[4]. The array becomes[0, 0, 0, 0, 0].The minimum number of operations required is 4.
The output is 4.
Constraints:
<code>1 <= queries.length <= 10<sup>5</sup></code>
queries[i].length == 2queries[i] == [l, r]<code>1 <= l < r <= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final LongminOperations(Array<IntArray> queries)-
-
Method Detail
-
minOperations
final Long minOperations(Array<IntArray> queries)
-
-
-
-