You are given a 0-indexed integer array arr and an integer k. The array arr is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.
You can do the following operation any number of times:
arr and increase or decrease it by 1.Return the minimum number of operations such that the sum of each subarray of length k is equal.
A subarray is a contiguous part of the array.
Example 1:
Input: arr = [1,4,1,3], k = 2 Output: 1 Explanation: we can do one operation on index 1 to make its value equal to 3. The array after the operation is [1,3,1,3] - Subarray starts at index 0 is [1, 3], and its sum is 4 - Subarray starts at index 1 is [3, 1], and its sum is 4 - Subarray starts at index 2 is [1, 3], and its sum is 4 - Subarray starts at index 3 is [3, 1], and its sum is 4
Example 2:
Input: arr = [2,5,5,7], k = 3 Output: 5 Explanation: we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5. The array after the operations is [5,5,5,5] - Subarray starts at index 0 is [5, 5, 5], and its sum is 15 - Subarray starts at index 1 is [5, 5, 5], and its sum is 15 - Subarray starts at index 2 is [5, 5, 5], and its sum is 15 - Subarray starts at index 3 is [5, 5, 5], and its sum is 15
Constraints:
1 <= k <= arr.length <= 1051 <= arr[i] <= 109Group elements by their indices modulo k to form independent equivalence classes. To equalize each group, transform them to their median value to minimize the number of operations needed.
The idea behind using median is that it minimizes the sum of absolute deviations from a central point.
In this solution, we group the elements by their indices modulo k. For each group, we calculate the median and convert all elements in that group to the median, summing the operations.
Java
Time Complexity: O(n log n) due to sorting when computing the median for each group.
Space Complexity: O(n) to store the intermediate groups.
This approach uses dynamic programming to maintain a cost structure as we equalize values effectively. By tracking the cost of bringing elements closer together, we manage the transitions efficiently while minimizing operations across different subarrays.
This solution uses dynamic programming to implicitly keep track of the costs as we compute the optimal transformation by groups using sorting.
Time Complexity: O(n log n) for sorting during value group calculation.
Space Complexity: O(n) for auxiliary storage of elements per group.
| Approach | Complexity |
|---|---|
| Median Based Optimization | Time Complexity: O(n log n) due to sorting when computing the median for each group. |
| Dynamic Programming Strategy | Time Complexity: O(n log n) for sorting during value group calculation. |
Longest Subarray with sum K | Brute - Better - Optimal | Generate Subarrays • take U forward • 891,822 views views
Watch 9 more video solutions →Practice Make K-Subarray Sums Equal with our built-in code editor and test cases.
Practice on FleetCode