Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. Ways to Make a Fair Array

Ways to Make a Fair Array

Count the positions whose removal leaves an array where the even-index sum equals the odd-index sum. A prefix sum problem about how deleting one element flips the parity of everything after it. Solve it below in Python or JavaScript, then read the walkthrough.

Solve it in your browserRead the solution
MediumArrayPrefix SumAsked at 84 companies incl. Meta, Microsoft, Phonepe

Problem

Call an array fair if the sum of the values at even indices equals the sum of the values at odd indices (indices start at 0).

You're given an integer array nums. You may delete exactly one element. After the deletion, the elements to its right shift left by one, so their indices change.

Return how many indices i there are such that deleting nums[i] leaves a fair array. An empty array counts as fair, since both sums are 0.

Example 1

Input
nums = [2, 1, 6, 4]
Output
1

Deleting index 1 leaves [2, 6, 4]: even indices sum to 2 + 4 = 6 and the odd index holds 6. No other deletion works.

Example 2

Input
nums = [1, 1, 1]
Output
3

Any deletion leaves [1, 1], which is fair.

Example 3

Input
nums = [3, 1, 2, 2]
Output
0

Deleting index 0, 1, 2 or 3 leaves [1, 2, 2], [3, 2, 2], [3, 1, 2] or [3, 1, 2]. None of them balance.

Constraints

  • 1 ≤ nums.length ≤ 10⁵
  • 0 ≤ nums[i] ≤ 10⁴
  • The answer is between 0 and nums.length.

Practice

1
2
3
4

Runs 13 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.

Get asked Ways to Make a Fair Array in a real interview?

Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.

Start Free Trial

How to solve Ways to Make a Fair Array

Pattern: Prefix sums split by parity

Deleting nums[i] does two different things. Elements before i keep their indices, so their even and odd sums are unchanged. Elements after i all shift left by one, so every even index becomes odd and every odd index becomes even.

That means the new even sum is (even sum before i) + (odd sum after i), and the new odd sum is (odd sum before i) + (even sum after i). If you know the four partial sums at every position, each deletion is a constant-time check. Keep running "before" sums as you walk, and get "after" sums by subtracting from the totals.

Approach 1: Delete and re-sum

For each index, build the array without it and add up its even and odd positions. Direct and easy to trust, but each check costs O(n).

Time O(n²) · Space O(n)

Approach 2: Prefix arrays

Precompute evenPrefix[i] and oddPrefix[i], the sums of even and odd indexed values in nums[0..i-1]. For each i, read the four partial sums from the arrays and compare. Linear, with two extra arrays.

Time O(n) · Space O(n)

Approach 3: Running sums with totals

  1. Compute evenTotal and oddTotal for the whole array. Start evenLeft = oddLeft = 0.
  2. For each i, remove nums[i] from its parity's total, so the totals now cover only indices after i.
  3. If evenLeft + oddTotal == oddLeft + evenTotal, count it.
  4. Add nums[i] to its parity's left sum and move on.

Time O(n) · Space O(1)

ApproachTimeSpace
Delete and re-sumO(n²)O(n)
Prefix arraysO(n)O(n)
Running sums with totalsO(n)O(1)

Solution code

Try the practice editor first. Both solutions pass every test above.

▸Ways to Make a Fair Array solution in Python
def ways_to_make_fair(nums: list[int]) -> int:
    even_total = sum(nums[0::2])
    odd_total = sum(nums[1::2])
    even_left = odd_left = 0
    count = 0
    for i, x in enumerate(nums):
        if i % 2 == 0:
            even_total -= x
        else:
            odd_total -= x
        # Everything right of i flips parity after the deletion.
        if even_left + odd_total == odd_left + even_total:
            count += 1
        if i % 2 == 0:
            even_left += x
        else:
            odd_left += x
    return count
▸Ways to Make a Fair Array solution in JavaScript
function waysToMakeFair(nums) {
  let evenTotal = 0;
  let oddTotal = 0;
  nums.forEach((x, i) => {
    if (i % 2 === 0) evenTotal += x;
    else oddTotal += x;
  });
  let evenLeft = 0;
  let oddLeft = 0;
  let count = 0;
  nums.forEach((x, i) => {
    if (i % 2 === 0) evenTotal -= x;
    else oddTotal -= x;
    // Everything right of i flips parity after the deletion.
    if (evenLeft + oddTotal === oddLeft + evenTotal) count++;
    if (i % 2 === 0) evenLeft += x;
    else oddLeft += x;
  });
  return count;
}

Common mistakes

  • Forgetting that every element after the deleted one switches parity. Comparing even-after with even-after gives the wrong answer.
  • Updating the left sums before the check, which wrongly counts the deleted element on the left side.
  • Subtracting the deleted value from the wrong total (it belongs to the total of its own index parity).
  • Treating a single-element array as a special case. Deleting it leaves an empty array, which is fair.

What interviewers look for

The whole problem hinges on one observation, so say it before coding: "deletion keeps the left part's parities and flips the right part's." Then write the balance formula with the four sums and the code follows.

Interviewers like to see you check the formula on a tiny case like [2, 1, 6, 4] deleting index 1, and to hear you explain why the O(1) space version works: the "after" sums are just totals minus what you've already passed.

Follow-up questions

What if you could delete any contiguous block of length L?

The left part keeps its parities and the right part flips only when L is odd. Use the same prefix sums by parity and check each window start in O(1), for O(n) overall.

How would you return the indices instead of the count?

Same loop; append i instead of incrementing a counter. Time stays O(n), and the output takes O(k) space for k fair deletions.

What if values can be negative?

Nothing changes. The method only adds and compares sums, so negative values work the same way. Watch for overflow in fixed-width languages if values and lengths are large.

Companies that ask Ways to Make a Fair Array

Ways to Make a Fair Array is tagged by 84 companies in our interview dataset. The score shows how often it comes up at each company.

  • Zipline100%
  • Dunzo100%
  • Verizon97%
  • Nbc91%
  • Bigcommerce90%
  • Adyen89%
  • Gofundme89%
  • Hackerrank89%
  • Basis Technologies88%
  • Bandwidth88%
  • 23andme88%
  • Cerner83%
  • Palantir82%
  • Nisum80%
  • Ford Motor80%
  • Electronic Arts80%
  • N2677%
  • Clever76%
  • Klaviyo74%
  • Fico73%
  • Verisk73%
  • Best Buy73%
  • Logmein72%
  • Olympus69%

and 60 more.

Practice next

  • Product of Array Except SelfMediumPrefix and suffix products
  • Subarray Sum Equals KMediumPrefix sums + hash map of counts
  • Meeting Rooms IIMediumSweep line over start and end events
  • Two SumEasyHash map of complements

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • AI Interview Copilot
  • Coding Interview Copilot
  • AI Interview Assistant
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.