Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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] <= 109Problem summary: 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: Pick any element from 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[1,4,1,3] 2
[2,5,5,7] 3
rotate-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2607: Make K-Subarray Sums Equal
class Solution {
public long makeSubKSumEqual(int[] arr, int k) {
int n = arr.length;
int g = gcd(n, k);
long ans = 0;
for (int i = 0; i < g; ++i) {
List<Integer> t = new ArrayList<>();
for (int j = i; j < n; j += g) {
t.add(arr[j]);
}
t.sort((a, b) -> a - b);
int mid = t.get(t.size() >> 1);
for (int x : t) {
ans += Math.abs(x - mid);
}
}
return ans;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #2607: Make K-Subarray Sums Equal
func makeSubKSumEqual(arr []int, k int) (ans int64) {
n := len(arr)
g := gcd(n, k)
for i := 0; i < g; i++ {
t := []int{}
for j := i; j < n; j += g {
t = append(t, arr[j])
}
sort.Ints(t)
mid := t[len(t)/2]
for _, x := range t {
ans += int64(abs(x - mid))
}
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
# Accepted solution for LeetCode #2607: Make K-Subarray Sums Equal
class Solution:
def makeSubKSumEqual(self, arr: List[int], k: int) -> int:
n = len(arr)
g = gcd(n, k)
ans = 0
for i in range(g):
t = sorted(arr[i:n:g])
mid = t[len(t) >> 1]
ans += sum(abs(x - mid) for x in t)
return ans
// Accepted solution for LeetCode #2607: Make K-Subarray Sums Equal
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2607: Make K-Subarray Sums Equal
// class Solution {
// public long makeSubKSumEqual(int[] arr, int k) {
// int n = arr.length;
// int g = gcd(n, k);
// long ans = 0;
// for (int i = 0; i < g; ++i) {
// List<Integer> t = new ArrayList<>();
// for (int j = i; j < n; j += g) {
// t.add(arr[j]);
// }
// t.sort((a, b) -> a - b);
// int mid = t.get(t.size() >> 1);
// for (int x : t) {
// ans += Math.abs(x - mid);
// }
// }
// return ans;
// }
//
// private int gcd(int a, int b) {
// return b == 0 ? a : gcd(b, a % b);
// }
// }
// Accepted solution for LeetCode #2607: Make K-Subarray Sums Equal
function makeSubKSumEqual(arr: number[], k: number): number {
const n = arr.length;
const g = gcd(n, k);
let ans = 0;
for (let i = 0; i < g; ++i) {
const t: number[] = [];
for (let j = i; j < n; j += g) {
t.push(arr[j]);
}
t.sort((a, b) => a - b);
const mid = t[t.length >> 1];
for (const x of t) {
ans += Math.abs(x - mid);
}
}
return ans;
}
function gcd(a: number, b: number): number {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.