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. You can choose exactly one index (0-indexed) and remove the element. Notice that the index of the elements may change after the removal.
For example, if nums = [6,1,7,4,1]:
1 results in nums = [6,7,4,1].2 results in nums = [6,1,4,1].4 results in nums = [6,1,7,4].An array is fair if the sum of the odd-indexed values equals the sum of the even-indexed values.
Return the number of indices that you could choose such that after the removal, nums is fair.
Example 1:
Input: nums = [2,1,6,4] Output: 1 Explanation: Remove index 0: [1,6,4] -> Even sum: 1 + 4 = 5. Odd sum: 6. Not fair. Remove index 1: [2,6,4] -> Even sum: 2 + 4 = 6. Odd sum: 6. Fair. Remove index 2: [2,1,4] -> Even sum: 2 + 4 = 6. Odd sum: 1. Not fair. Remove index 3: [2,1,6] -> Even sum: 2 + 6 = 8. Odd sum: 1. Not fair. There is 1 index that you can remove to make nums fair.
Example 2:
Input: nums = [1,1,1] Output: 3 Explanation: You can remove any index and the remaining array is fair.
Example 3:
Input: nums = [1,2,3] Output: 0 Explanation: You cannot make a fair array after removing any index.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 104Problem summary: You are given an integer array nums. You can choose exactly one index (0-indexed) and remove the element. Notice that the index of the elements may change after the removal. For example, if nums = [6,1,7,4,1]: Choosing to remove index 1 results in nums = [6,7,4,1]. Choosing to remove index 2 results in nums = [6,1,4,1]. Choosing to remove index 4 results in nums = [6,1,7,4]. An array is fair if the sum of the odd-indexed values equals the sum of the even-indexed values. Return the number of indices that you could choose such that after the removal, nums is fair.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,1,6,4]
[1,1,1]
[1,2,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1664: Ways to Make a Fair Array
class Solution {
public int waysToMakeFair(int[] nums) {
int s1 = 0, s2 = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
s1 += i % 2 == 0 ? nums[i] : 0;
s2 += i % 2 == 1 ? nums[i] : 0;
}
int t1 = 0, t2 = 0;
int ans = 0;
for (int i = 0; i < n; ++i) {
int v = nums[i];
ans += i % 2 == 0 && t2 + s1 - t1 - v == t1 + s2 - t2 ? 1 : 0;
ans += i % 2 == 1 && t2 + s1 - t1 == t1 + s2 - t2 - v ? 1 : 0;
t1 += i % 2 == 0 ? v : 0;
t2 += i % 2 == 1 ? v : 0;
}
return ans;
}
}
// Accepted solution for LeetCode #1664: Ways to Make a Fair Array
func waysToMakeFair(nums []int) (ans int) {
var s1, s2, t1, t2 int
for i, v := range nums {
if i%2 == 0 {
s1 += v
} else {
s2 += v
}
}
for i, v := range nums {
if i%2 == 0 && t2+s1-t1-v == t1+s2-t2 {
ans++
}
if i%2 == 1 && t2+s1-t1 == t1+s2-t2-v {
ans++
}
if i%2 == 0 {
t1 += v
} else {
t2 += v
}
}
return
}
# Accepted solution for LeetCode #1664: Ways to Make a Fair Array
class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
s1, s2 = sum(nums[::2]), sum(nums[1::2])
ans = t1 = t2 = 0
for i, v in enumerate(nums):
ans += i % 2 == 0 and t2 + s1 - t1 - v == t1 + s2 - t2
ans += i % 2 == 1 and t2 + s1 - t1 == t1 + s2 - t2 - v
t1 += v if i % 2 == 0 else 0
t2 += v if i % 2 == 1 else 0
return ans
// Accepted solution for LeetCode #1664: Ways to Make a Fair Array
struct Solution;
impl Solution {
fn ways_to_make_fair(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut left_odd = vec![0; n];
let mut left_even = vec![0; n];
let mut right_odd = vec![0; n];
let mut right_even = vec![0; n];
let mut odd_sum = 0;
let mut even_sum = 0;
for i in 0..n {
left_odd[i] = odd_sum;
left_even[i] = even_sum;
if i % 2 == 0 {
even_sum += nums[i];
} else {
odd_sum += nums[i];
}
}
odd_sum = 0;
even_sum = 0;
for i in (0..n).rev() {
right_odd[i] = odd_sum;
right_even[i] = even_sum;
if i % 2 == 0 {
even_sum += nums[i];
} else {
odd_sum += nums[i];
}
}
let mut res = 0;
for i in 0..n {
if left_odd[i] + right_even[i] == left_even[i] + right_odd[i] {
res += 1;
}
}
res
}
}
#[test]
fn test() {
let nums = vec![2, 1, 6, 4];
let res = 1;
assert_eq!(Solution::ways_to_make_fair(nums), res);
let nums = vec![1, 1, 1];
let res = 3;
assert_eq!(Solution::ways_to_make_fair(nums), res);
let nums = vec![1, 2, 3];
let res = 0;
assert_eq!(Solution::ways_to_make_fair(nums), res);
}
// Accepted solution for LeetCode #1664: Ways to Make a Fair Array
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1664: Ways to Make a Fair Array
// class Solution {
// public int waysToMakeFair(int[] nums) {
// int s1 = 0, s2 = 0;
// int n = nums.length;
// for (int i = 0; i < n; ++i) {
// s1 += i % 2 == 0 ? nums[i] : 0;
// s2 += i % 2 == 1 ? nums[i] : 0;
// }
// int t1 = 0, t2 = 0;
// int ans = 0;
// for (int i = 0; i < n; ++i) {
// int v = nums[i];
// ans += i % 2 == 0 && t2 + s1 - t1 - v == t1 + s2 - t2 ? 1 : 0;
// ans += i % 2 == 1 && t2 + s1 - t1 == t1 + s2 - t2 - v ? 1 : 0;
// t1 += i % 2 == 0 ? v : 0;
// t2 += i % 2 == 1 ? v : 0;
// }
// 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.