Ways to Make a Fair Array - Problem
Ways to Make a Fair Array
You are given an integer array
Here's what happens when you remove an element:
• Choose exactly one index (0-indexed) and remove that element
• All elements after the removed index shift left by one position
• The array becomes shorter by one element
What makes an array fair?
An array is fair if the sum of elements at odd indices equals the sum of elements at even indices.
Example walkthrough:
Given
• Remove index 1 →
• Remove index 2 →
• Remove index 4 →
Return the count of indices whose removal would make the array fair.
You are given an integer array
nums. Your goal is to find how many elements you can remove to make the array "fair".Here's what happens when you remove an element:
• Choose exactly one index (0-indexed) and remove that element
• All elements after the removed index shift left by one position
• The array becomes shorter by one element
What makes an array fair?
An array is fair if the sum of elements at odd indices equals the sum of elements at even indices.
Example walkthrough:
Given
nums = [6,1,7,4,1]:• Remove index 1 →
[6,7,4,1] → even sum: 6+4=10, odd sum: 7+1=8 → not fair• Remove index 2 →
[6,1,4,1] → even sum: 6+4=10, odd sum: 1+1=2 → not fair• Remove index 4 →
[6,1,7,4] → even sum: 6+7=13, odd sum: 1+4=5 → not fairReturn the count of indices whose removal would make the array fair.
Input & Output
example_1.py — Basic Case
$
Input:
[2,1,6,4]
›
Output:
1
💡 Note:
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. Only 1 way to make it fair.
example_2.py — Multiple Solutions
$
Input:
[1,1,1]
›
Output:
3
💡 Note:
Remove index 0: [1,1] → even_sum=1, odd_sum=1, fair. Remove index 1: [1,1] → even_sum=1, odd_sum=1, fair. Remove index 2: [1,1] → even_sum=1, odd_sum=1, fair. All 3 removals make it fair since all elements are equal.
example_3.py — Single Element
$
Input:
[1]
›
Output:
1
💡 Note:
Remove the only element: [] → even_sum=0, odd_sum=0, fair by definition (empty sums are equal). So there's 1 way to make it fair.
Visualization
Tap to expand
Understanding the Visualization
1
Initial Setup
Weights [6,1,7,4,1] are placed: Left pan gets 6,7,1 (even indices), Right pan gets 1,4 (odd indices)
2
Remove Weight
When we remove weight at position 1 (value=1), all weights after it jump to opposite pans
3
Recalculate Balance
New arrangement: Left pan has 6,4 (total=10), Right pan has 7,1 (total=8). Not balanced!
4
Try All Positions
Repeat this process for each position to find all balanced configurations
Key Takeaway
🎯 Key Insight: When you remove an element, all subsequent elements flip their index parity (even→odd, odd→even), allowing O(1) calculation using prefix sums!
Time & Space Complexity
Time Complexity
O(n²)
For each of n elements, we iterate through up to n-1 elements to calculate sums
⚠ Quadratic Growth
Space Complexity
O(n)
We create a new array of size n-1 for each removal simulation
⚡ Linearithmic Space
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 104
- The array contains at least one element
- Index-based: Array uses 0-based indexing
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code