1664. Ways to Make a Fair Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1664

class Solution:
    def waysToMakeFair(self, nums: List[int]) -> int:
        s1, s2 = sum(nums[::2]), sum(nums[1::2])
        res = t1 = t2 = 0
        for i, v in enumerate(nums):
            res += i % 2 == 0 and t2 + s1 - t1 - v == t1 + s2 - t2
            res += 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 res