Watch 2 video solutions for Minimum Operations to Equalize Subarrays, a hard level problem involving Array, Math, Binary Search. This walkthrough by Rajan Keshari ( CSE - IIT Dhanbad ) has 1,727 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums and an integer k.
In one operation, you can increase or decrease any element of nums by exactly k.
You are also given a 2D integer array queries, where each queries[i] = [li, ri].
For each query, find the minimum number of operations required to make all elements in the subarray nums[li..ri] equal. If it is impossible, the answer for that query is -1.
Return an array ans, where ans[i] is the answer for the ith query.
Example 1:
Input: nums = [1,4,7], k = 3, queries = [[0,1],[0,2]]
Output: [1,2]
Explanation:
One optimal set of operations:
i |
[li, ri] |
nums[li..ri] |
Possibility | Operations | Finalnums[li..ri] |
ans[i] |
|---|---|---|---|---|---|---|
| 0 | [0, 1] | [1, 4] | Yes | nums[0] + k = 1 + 3 = 4 = nums[1] |
[4, 4] | 1 |
| 1 | [0, 2] | [1, 4, 7] | Yes | nums[0] + k = 1 + 3 = 4 = nums[1] |
[4, 4, 4] | 2 |
Thus, ans = [1, 2].
Example 2:
Input: nums = [1,2,4], k = 2, queries = [[0,2],[0,0],[1,2]]
Output: [-1,0,1]
Explanation:
One optimal set of operations:
i |
[li, ri] |
nums[li..ri] |
Possibility | Operations | Finalnums[li..ri] |
ans[i] |
|---|---|---|---|---|---|---|
| 0 | [0, 2] | [1, 2, 4] | No | - | [1, 2, 4] | -1 |
| 1 | [0, 0] | [1] | Yes | Already equal | [1] | 0 |
| 2 | [1, 2] | [2, 4] | Yes | nums[1] + k = 2 + 2 = 4 = nums[2] |
[4, 4] | 1 |
Thus, ans = [-1, 0, 1].
Constraints:
1 <= n == nums.length <= 4 × 1041 <= nums[i] <= 109āāāāāāā1 <= k <= 1091 <= queries.length <= 4 × 104āāāāāāāqueries[i] = [li, ri]0 <= li <= ri <= n - 1Problem Overview: You are given an array and must determine the minimum number of operations required to make elements within selected subarrays equal. Each operation typically adjusts values so that all numbers in a range match a target value while minimizing total cost.
Approach 1: Brute Force with Prefix Sums (Time: O(n²), Space: O(n))
Enumerate every possible subarray using two nested loops. For each subarray, determine the value all elements should be converted to (often the maximum or median depending on the allowed operation) and compute the cost of transforming the elements. A prefix sum array helps calculate range sums quickly, so the cost of converting the subarray can be computed in constant time after scanning the range. This approach is straightforward but too slow for large inputs because the number of subarrays is O(n²).
Approach 2: Binary Search on Target Value (Time: O(n log n), Space: O(n))
Instead of testing every possible target value directly, apply binary search on the value that equalizes the subarray. For each candidate target, compute how many operations are required to transform elements using prefix sums. The key insight is that the cost function becomes monotonic when operations only increase or decrease values, which allows binary search to converge on the minimal feasible target. This reduces repeated computation across ranges and works well when the value domain is large.
Approach 3: Segment Tree with Range Queries (Time: O(n log n), Space: O(n))
Use a segment tree to support fast range queries such as maximum value or sum within a subarray. While scanning the array, query the segment tree to determine the target value that all elements should match and compute the cost using prefix sums. The segment tree allows you to update or query ranges in O(log n) time, which keeps the total complexity manageable even when evaluating many subarrays. This approach is common in advanced array optimization problems where both range aggregation and dynamic updates are required.
Recommended for interviews: Interviewers expect the optimized solution using binary search combined with efficient range queries. Starting with the brute force explanation demonstrates understanding of the cost calculation. Moving to a segment tree or binary-search-based optimization shows the ability to reduce repeated work and achieve O(n log n) performance, which is necessary for hard array problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force with Prefix Sums | O(n²) | O(n) | Small arrays or when demonstrating baseline logic in interviews |
| Binary Search on Target Value | O(n log n) | O(n) | When the cost function is monotonic and you can test feasibility efficiently |
| Segment Tree with Range Queries | O(n log n) | O(n) | Large inputs requiring frequent range max or sum queries |