3495. Minimum Operations to Make Array Elements Zero LeetCode Solution

In this guide, you will get 3495. Minimum Operations to Make Array Elements Zero LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make Array Elements Zero problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Make Array Elements Zero solution in C++
  4. Minimum Operations to Make Array Elements Zero solution in Java
  5. Minimum Operations to Make Array Elements Zero solution in Python
  6. Additional Resources
3495. Minimum Operations to Make Array Elements Zero LeetCode Solution image

Problem Statement of Minimum Operations to Make Array Elements Zero

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 a and b from the array.
Replace them with floor(a / 4) and floor(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] and nums[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] and nums[2]. The array becomes [0, 3, 1].
In the second operation, select nums[1] and nums[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] and nums[3]. The array becomes [0, 3, 4, 1, 6].
In the second operation, select nums[2] and nums[4]. The array becomes [0, 3, 1, 1, 1].
In the third operation, select nums[1] and nums[2]. The array becomes [0, 0, 0, 1, 1].
In the fourth operation, select nums[3] and nums[4]. The array becomes [0, 0, 0, 0, 0].
The minimum number of operations required is 4.

The output is 4.

Constraints:

1 <= queries.length <= 105
queries[i].length == 2
queries[i] == [l, r]
1 <= l < r <= 109

Complexity Analysis

  • Time Complexity: O(q)
  • Space Complexity: O(1)

3495. Minimum Operations to Make Array Elements Zero LeetCode Solution in C++

class Solution {
 public:
  long long minOperations(vector<vector<int>>& queries) {
    long ans = 0;
    for (const vector<int>& query : queries) {
      const int l = query[0];
      const int r = query[1];
      ans += (getOperations(r) - getOperations(l - 1) + 1) / 2;
    }
    return ans;
  }

 private:
  // Returns the number of operations required for [1, n].
  long getOperations(int n) {
    long res = 0;
    int ops = 0;
    for (int powerOfFour = 1; powerOfFour <= n; powerOfFour *= 4) {
      const int l = powerOfFour;
      const int r = min(n, powerOfFour * 4 - 1);
      res += static_cast<long>(r - l + 1) * ++ops;
    }
    return res;
  }
};
/* code provided by PROGIEZ */

3495. Minimum Operations to Make Array Elements Zero LeetCode Solution in Java

class Solution {
  public long minOperations(int[][] queries) {
    long ans = 0;
    for (int[] query : queries) {
      final int l = query[0];
      final int r = query[1];
      ans += (getOperations(r) - getOperations(l - 1) + 1) / 2;
    }
    return ans;
  }

  // Returns the number of operations required for [1, n].
  private long getOperations(int n) {
    long res = 0;
    int ops = 0;
    for (int powerOfFour = 1; powerOfFour <= n; powerOfFour *= 4) {
      final int l = powerOfFour;
      final int r = Math.min(n, powerOfFour * 4 - 1);
      res += (long) (r - l + 1) * ++ops;
    }
    return res;
  }
}
// code provided by PROGIEZ

3495. Minimum Operations to Make Array Elements Zero LeetCode Solution in Python

class Solution:
  def minOperations(self, queries: list[list[int]]) -> int:
    return sum((self._getOperations(r) - self._getOperations(l - 1) + 1) // 2
               for l, r in queries)

  def _getOperations(self, n: int) -> int:
    """Returns the number of operations required for [1, n]."""
    res = 0
    ops = 0
    powerOfFour = 1
    while powerOfFour <= n:
      l = powerOfFour
      r = min(n, powerOfFour * 4 - 1)
      ops += 1
      res += (r - l + 1) * ops
      powerOfFour *= 4
    return res
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.