
MD ARIFUL HAQUE3505. Minimum Operations to Make Elements Within K Subarrays Equal Difficulty: Hard Topics:...
3505. Minimum Operations to Make Elements Within K Subarrays Equal
Difficulty: Hard
Topics: Principal, Array, Hash Table, Math, Dynamic Programming, Sliding Window, Heap (Priority Queue), Weekly Contest 443
You are given an integer array nums and two integers, x and k. You can perform the following operation any number of times (including zero):
nums by 1.Return the minimum number of operations needed to have at least k non-overlapping subarrays1 of size exactly x in nums, where all elements within each subarray are equal.
Example 1:
nums[1] and use 2 operations to subtract 2 from nums[3]. The resulting array is [5, 1, 1, 1, 7, 3, 6, 4, -1].nums[5] and use 2 operations to subtract 2 from nums[6]. The resulting array is [5, 1, 1, 1, 7, 4, 4, 4, -1].[1, 1, 1] (from indices 1 to 3) and [4, 4, 4] (from indices 5 to 7) are equal. Since 8 total operations were used, 8 is the output.Example 2:
Explanation:
nums[4]. The resulting array is [9, -2, -2, -2, -2, 5].[-2, -2] (from indices 1 to 2) and [-2, -2] (from indices 3 to 4) are equal. Since 3 operations were used, 3 is the output.
Example 3:
Input: nums = [1,1,1,1,1], x = 2, k = 2
Output: 0
Example 4:
Example 5:
Example 6:
Example 7:
Example 8:
Example 9:
Example 10:
Example 11:
Example 12:
Constraints:
2 <= nums.length <= 10⁵-10⁶ <= nums[i] <= 10⁶2 <= x <= nums.length1 <= k <= 152 <= k * x <= nums.lengthHint:
Similar Questions:
Solution:
We solve this problem by first precomputing the minimum cost to make every x-sized window uniform (using the median as target), then applying a prefix DP to select k non-overlapping windows with minimum total cost. Window costs are computed in O(log n) per position using two Fenwick trees (BITs) — one for counts, one for sums — over coordinate-compressed values, enabling efficient sliding-window median and cost queries.
nums values so BIT indices are bounded by m ≤ n.bitCount, bitSum) maintain the multiset of values inside the current window of size x.x elements into both BITs.0 … n-x, do:
(x+1)/2-th smallest) via BIT kth query.sum |aᵢ − median| using prefix count/sum at the median rank.nums[start], insert nums[start+x], updating both BITs and the running window sum.prev[i] = min cost using chosen-1 windows among first i elements.
cur[i] = min(cur[i-1], prev[i-x] + cost[i-x]).chosen = 1…k, return prev[n].Let's implement this solution in PHP: 3505. Minimum Operations to Make Elements Within K Subarrays Equal
<?php
/**
* @param Integer[] $nums
* @param Integer $x
* @param Integer $k
* @return Integer
*/
function minOperations(array $nums, int $x, int $k): int
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $bitCount
* @param $bitSum
* @param $idx
* @param $deltaCount
* @param $deltaSum
* @param $m
* @return void
*/
function add(&$bitCount, &$bitSum, $idx, $deltaCount, $deltaSum, $m): void
{
...
...
...
/**
* go to ./solution.php
*/
}
/**
* @param $bitCount
* @param $k
* @param $m
* @param $highestPower
* @return int|mixed
*/
function kth($bitCount, $k, $m, $highestPower): mixed
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo minOperations([5,-2,1,3,7,3,6,4,-1], 3, 2) . "\n"; // Output: 8
echo minOperations([9,-2,-2,-2,1,5], 2, 2) . "\n"; // Output: 3
echo minOperations([1,1,1,1,1], 2, 2) . "\n"; // Output: 0
echo minOperations([-5,-5,-5,-5,-5], 2, 2) . "\n"; // Output: 0
echo minOperations([1,2,3,4], 2, 2) . "\n"; // Output: 2
echo minOperations([5,1,3], 3, 1) . "\n"; // Output: 4
echo minOperations([3,1,2,4,5,6], 2, 3) . "\n"; // Output: 5
echo minOperations([1,2,3,4,5,6,7,8], 4, 2) . "\n"; // Output: 8
echo minOperations([-1000000,1000000,-1000000,1000000], 2, 2) . "\n"; // Output: 4000000
echo minOperations([0,0,0,100,100,0,0,0], 3, 2) . "\n"; // Output: 0
echo minOperations([10,-10,10,-10,10,-10], 2, 3) . "\n"; // Output: 60
echo minOperations([1,1,2,2,3,3,4,4], 4, 2) . "\n"; // Output: 4
?>
sum |aᵢ − m| is minimized at the median (hint 1). Any even-sized window can use the lower median without loss.O(log n) while sliding. Two BITs give count and sum for the rank query, and prefix sums up to the median let us split the window into left/right halves.cost = median * cntLE − sumLE + (windowSum − sumLE) − median * (x − cntLE)(median − aᵢ).(aᵢ − median).i−1 (starting at i−x) consumes exactly those x slots, so prev[i−x] uses positions before the window starts — guaranteeing non-overlap by construction.min over exactly k equals min over ≥ k. The constraint k*x ≤ n guarantees feasibility.x: Using the lower median still yields the minimum L1 cost, so medianRank = (x+1)/2 is correct for both odd and even window sizes.• Time Complexity:
O(n log n)
O(x log n)
n − x + 1 steps, each O(log n)): O(n log n)
O(k · n) ≤ O(15 · 10⁵)
O(n log n + nk)
• Space Complexity:bitCount, bitSum arrays of size m + 1: O(n)
vals, map, nums, prev, cur, cost: O(n)
O(n)Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Subarray: A subarray is a contiguous non-empty sequence of elements within an array. ↩