Minimum Operations to Make Array Elements Zero

hard array math base-4

Problem

Each query [l, r] defines the array nums = [l, l+1, …, r]. One operation picks two elements a and b and replaces them with floor(a / 4) and floor(b / 4). Find the minimum operations to make every element zero, and return the sum of those minimums over all queries.

A single value x needs cost(x) = floor(log₄ x) + 1 divides to reach 0. If S is the total cost of the whole array, each operation clears 2 units of cost, so the answer for a query is ceil(S / 2) (the largest single cost never dominates because l < r).

Inputqueries = [[1,2],[2,4]]
Output3
[1,2] costs 1+1=2 → ceil(2/2)=1. [2,3,4] costs 1+1+2=4 → ceil(4/2)=2. Total 1+2=3.

def minOperations(queries):
    def cost(x):                       # /4 divides to zero one value
        c = 0
        while x > 0:
            x //= 4
            c += 1
        return c
    def g(n):                          # sum of cost(1..n) by base-4 buckets
        if n <= 0:
            return 0
        total, p, k = 0, 1, 0
        while p <= n:
            hi = min(n, p * 4 - 1)     # last value with this cost
            total += (hi - p + 1) * (k + 1)
            p *= 4
            k += 1
        return total
    ans = 0
    for l, r in queries:
        s = g(r) - g(l - 1)            # total cost over [l, r]
        ans += (s + 1) // 2            # each op clears 2 units of cost
    return ans
function minOperations(queries) {
  function cost(x) {                    // /4 divides to zero one value
    let c = 0;
    while (x > 0) { x = Math.floor(x / 4); c++; }
    return c;
  }
  function g(n) {                       // sum of cost(1..n) by base-4 buckets
    if (n <= 0) return 0;
    let total = 0, p = 1, k = 0;
    while (p <= n) {
      const hi = Math.min(n, p * 4 - 1); // last value with this cost
      total += (hi - p + 1) * (k + 1);
      p *= 4;
      k++;
    }
    return total;
  }
  let ans = 0;
  for (const [l, r] of queries) {
    const s = g(r) - g(l - 1);          // total cost over [l, r]
    ans += Math.floor((s + 1) / 2);     // each op clears 2 units of cost
  }
  return ans;
}
long minOperations(int[][] queries) {
    long ans = 0;
    for (int[] q : queries) {
        long s = g(q[1]) - g(q[0] - 1); // total cost over [l, r]
        ans += (s + 1) / 2;             // each op clears 2 units of cost
    }
    return ans;
}
long cost(long x) {                     // /4 divides to zero one value
    long c = 0;
    while (x > 0) { x /= 4; c++; }
    return c;
}
long g(long n) {                        // sum of cost(1..n) by base-4 buckets
    if (n <= 0) return 0;
    long total = 0, p = 1, k = 0;
    while (p <= n) {
        long hi = Math.min(n, p * 4 - 1); // last value with this cost
        total += (hi - p + 1) * (k + 1);
        p *= 4;
        k++;
    }
    return total;
}
long long g(long long n) {               // sum of cost(1..n) by base-4 buckets
    if (n <= 0) return 0;
    long long total = 0, p = 1, k = 0;
    while (p <= n) {
        long long hi = min(n, p * 4 - 1); // last value with this cost
        total += (hi - p + 1) * (k + 1);
        p *= 4;
        k++;
    }
    return total;
}
long long minOperations(vector<vector<int>>& queries) {
    long long ans = 0;
    for (auto& q : queries) {
        long long s = g(q[1]) - g(q[0] - 1); // total cost over [l, r]
        ans += (s + 1) / 2;              // each op clears 2 units of cost
    }
    return ans;
}
Time: O(Q · log r) Space: O(1)