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 an integer array nums of even length n and an integer limit. In one move, you can replace any integer from nums with another integer between 1 and limit, inclusive.
The array nums is complementary if for all indices i (0-indexed), nums[i] + nums[n - 1 - i] equals the same number. For example, the array [1,2,3,4] is complementary because for all indices i, nums[i] + nums[n - 1 - i] = 5.
Return the minimum number of moves required to make nums complementary.
Example 1:
Input: nums = [1,2,4,3], limit = 4 Output: 1 Explanation: In 1 move, you can change nums to [1,2,2,3] (underlined elements are changed). nums[0] + nums[3] = 1 + 3 = 4. nums[1] + nums[2] = 2 + 2 = 4. nums[2] + nums[1] = 2 + 2 = 4. nums[3] + nums[0] = 3 + 1 = 4. Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
Example 2:
Input: nums = [1,2,2,1], limit = 2 Output: 2 Explanation: In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 > limit.
Example 3:
Input: nums = [1,2,1,2], limit = 2 Output: 0 Explanation: nums is already complementary.
Constraints:
n == nums.length2 <= n <= 1051 <= nums[i] <= limit <= 105n is even.Problem summary: You are given an integer array nums of even length n and an integer limit. In one move, you can replace any integer from nums with another integer between 1 and limit, inclusive. The array nums is complementary if for all indices i (0-indexed), nums[i] + nums[n - 1 - i] equals the same number. For example, the array [1,2,3,4] is complementary because for all indices i, nums[i] + nums[n - 1 - i] = 5. Return the minimum number of moves required to make nums complementary.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,4,3] 4
[1,2,2,1] 2
[1,2,1,2] 2
zero-array-transformation-ii)zero-array-transformation-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1674: Minimum Moves to Make Array Complementary
class Solution {
public int minMoves(int[] nums, int limit) {
int[] d = new int[2 * limit + 2];
int n = nums.length;
for (int i = 0; i < n / 2; ++i) {
int x = Math.min(nums[i], nums[n - i - 1]);
int y = Math.max(nums[i], nums[n - i - 1]);
d[2] += 2;
d[x + 1] -= 2;
d[x + 1] += 1;
d[x + y] -= 1;
d[x + y + 1] += 1;
d[y + limit + 1] -= 1;
d[y + limit + 1] += 2;
}
int ans = n;
for (int i = 2, s = 0; i < d.length; ++i) {
s += d[i];
ans = Math.min(ans, s);
}
return ans;
}
}
// Accepted solution for LeetCode #1674: Minimum Moves to Make Array Complementary
func minMoves(nums []int, limit int) int {
n := len(nums)
d := make([]int, 2*limit+2)
for i := 0; i < n/2; i++ {
x, y := nums[i], nums[n-1-i]
if x > y {
x, y = y, x
}
d[2] += 2
d[x+1] -= 2
d[x+1] += 1
d[x+y] -= 1
d[x+y+1] += 1
d[y+limit+1] -= 1
d[y+limit+1] += 2
}
ans, s := n, 0
for _, x := range d[2:] {
s += x
ans = min(ans, s)
}
return ans
}
# Accepted solution for LeetCode #1674: Minimum Moves to Make Array Complementary
class Solution:
def minMoves(self, nums: List[int], limit: int) -> int:
d = [0] * (2 * limit + 2)
n = len(nums)
for i in range(n // 2):
x, y = nums[i], nums[-i - 1]
if x > y:
x, y = y, x
d[2] += 2
d[x + 1] -= 2
d[x + 1] += 1
d[x + y] -= 1
d[x + y + 1] += 1
d[y + limit + 1] -= 1
d[y + limit + 1] += 2
return min(accumulate(d[2:]))
// Accepted solution for LeetCode #1674: Minimum Moves to Make Array Complementary
struct Solution;
impl Solution {
fn min_moves(nums: Vec<i32>, limit: i32) -> i32 {
let n = nums.len();
let limit = limit as usize;
let mut delta = vec![0; limit * 2 + 2];
for i in 0..n / 2 {
let a = nums[i] as usize;
let b = nums[n - 1 - i] as usize;
delta[2] += 2;
delta[a.min(b) + 1] -= 1;
delta[a + b] -= 1;
delta[a + b + 1] += 1;
delta[a.max(b) + limit + 1] += 1;
}
let mut res = std::i32::MAX;
let mut cur = 0;
for sum in 2..=limit * 2 {
cur += delta[sum];
res = res.min(cur);
}
res
}
}
#[test]
fn test() {
let nums = vec![1, 2, 4, 3];
let limit = 4;
let res = 1;
assert_eq!(Solution::min_moves(nums, limit), res);
let nums = vec![1, 2, 2, 1];
let limit = 2;
let res = 2;
assert_eq!(Solution::min_moves(nums, limit), res);
let nums = vec![1, 2, 1, 2];
let limit = 2;
let res = 0;
assert_eq!(Solution::min_moves(nums, limit), res);
}
// Accepted solution for LeetCode #1674: Minimum Moves to Make Array Complementary
function minMoves(nums: number[], limit: number): number {
const n = nums.length;
const d: number[] = Array(limit * 2 + 2).fill(0);
for (let i = 0; i < n >> 1; ++i) {
const x = Math.min(nums[i], nums[n - 1 - i]);
const y = Math.max(nums[i], nums[n - 1 - i]);
d[2] += 2;
d[x + 1] -= 2;
d[x + 1] += 1;
d[x + y] -= 1;
d[x + y + 1] += 1;
d[y + limit + 1] -= 1;
d[y + limit + 1] += 2;
}
let ans = n;
let s = 0;
for (let i = 2; i < d.length; ++i) {
s += d[i];
ans = Math.min(ans, s);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.