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 two integer arrays arr and brr of length n, and an integer k. You can perform the following operations on arr any number of times:
arr into any number of contiguous subarrays and rearrange these subarrays in any order. This operation has a fixed cost of k.Choose any element in arr and add or subtract a positive integer x to it. The cost of this operation is x.
Return the minimum total cost to make arr equal to brr.
Example 1:
Input: arr = [-7,9,5], brr = [7,-2,-5], k = 2
Output: 13
Explanation:
arr into two contiguous subarrays: [-7] and [9, 5] and rearrange them as [9, 5, -7], with a cost of 2.arr[0]. The array becomes [7, 5, -7]. The cost of this operation is 2.arr[1]. The array becomes [7, -2, -7]. The cost of this operation is 7.arr[2]. The array becomes [7, -2, -5]. The cost of this operation is 2.The total cost to make the arrays equal is 2 + 2 + 7 + 2 = 13.
Example 2:
Input: arr = [2,1], brr = [2,1], k = 0
Output: 0
Explanation:
Since the arrays are already equal, no operations are needed, and the total cost is 0.
Constraints:
1 <= arr.length == brr.length <= 1050 <= k <= 2 * 1010-105 <= arr[i] <= 105-105 <= brr[i] <= 105Problem summary: You are given two integer arrays arr and brr of length n, and an integer k. You can perform the following operations on arr any number of times: Split arr into any number of contiguous subarrays and rearrange these subarrays in any order. This operation has a fixed cost of k. Choose any element in arr and add or subtract a positive integer x to it. The cost of this operation is x. Return the minimum total cost to make arr equal to brr.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[-7,9,5] [7,-2,-5] 2
[2,1] [2,1] 0
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3424: Minimum Cost to Make Arrays Identical
class Solution {
public long minCost(int[] arr, int[] brr, long k) {
long c1 = calc(arr, brr);
Arrays.sort(arr);
Arrays.sort(brr);
long c2 = calc(arr, brr) + k;
return Math.min(c1, c2);
}
private long calc(int[] arr, int[] brr) {
long ans = 0;
for (int i = 0; i < arr.length; ++i) {
ans += Math.abs(arr[i] - brr[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #3424: Minimum Cost to Make Arrays Identical
func minCost(arr []int, brr []int, k int64) int64 {
calc := func(a, b []int) (ans int64) {
for i := range a {
ans += int64(abs(a[i] - b[i]))
}
return
}
c1 := calc(arr, brr)
sort.Ints(arr)
sort.Ints(brr)
c2 := calc(arr, brr) + k
return min(c1, c2)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3424: Minimum Cost to Make Arrays Identical
class Solution:
def minCost(self, arr: List[int], brr: List[int], k: int) -> int:
c1 = sum(abs(a - b) for a, b in zip(arr, brr))
arr.sort()
brr.sort()
c2 = k + sum(abs(a - b) for a, b in zip(arr, brr))
return min(c1, c2)
// Accepted solution for LeetCode #3424: Minimum Cost to Make Arrays Identical
impl Solution {
pub fn min_cost(mut arr: Vec<i32>, mut brr: Vec<i32>, k: i64) -> i64 {
let c1: i64 = arr
.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum();
arr.sort_unstable();
brr.sort_unstable();
let c2: i64 = k + arr
.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum::<i64>();
c1.min(c2)
}
}
// Accepted solution for LeetCode #3424: Minimum Cost to Make Arrays Identical
function minCost(arr: number[], brr: number[], k: number): number {
const calc = (a: number[], b: number[]) => {
let ans = 0;
for (let i = 0; i < a.length; ++i) {
ans += Math.abs(a[i] - b[i]);
}
return ans;
};
const c1 = calc(arr, brr);
arr.sort((a, b) => a - b);
brr.sort((a, b) => a - b);
const c2 = calc(arr, brr) + k;
return Math.min(c1, c2);
}
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: 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.