class Solution:
def waysToMakeFair(self, nums):
n = len(nums)
total_even = 0
total_odd = 0
for i in range(n):
if i % 2 == 0:
total_even += nums[i]
else:
total_odd += nums[i]
res = 0
left_even = 0
left_odd = 0
for i in range(n):
if i % 2 == 0:
total_even -= nums[i]
else:
total_odd -= nums[i]
if left_even + total_odd == left_odd + total_even:
res += 1
if i % 2 == 0:
left_even += nums[i]
else:
left_odd += nums[i]
return res
class Solution {
public:
int waysToMakeFair(vector<int>& nums) {
int n = nums.size();
int total_even = 0, total_odd = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
total_even += nums[i];
else
total_odd += nums[i];
}
int res = 0, left_even = 0, left_odd = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
total_even -= nums[i];
else
total_odd -= nums[i];
if (left_even + total_odd == left_odd + total_even)
++res;
if (i % 2 == 0)
left_even += nums[i];
else
left_odd += nums[i];
}
return res;
}
};
class Solution {
public int waysToMakeFair(int[] nums) {
int n = nums.length;
int totalEven = 0, totalOdd = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
totalEven += nums[i];
else
totalOdd += nums[i];
}
int res = 0, leftEven = 0, leftOdd = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0)
totalEven -= nums[i];
else
totalOdd -= nums[i];
if (leftEven + totalOdd == leftOdd + totalEven)
++res;
if (i % 2 == 0)
leftEven += nums[i];
else
leftOdd += nums[i];
}
return res;
}
}
var waysToMakeFair = function(nums) {
let n = nums.length;
let totalEven = 0, totalOdd = 0;
for (let i = 0; i < n; ++i) {
if (i % 2 === 0)
totalEven += nums[i];
else
totalOdd += nums[i];
}
let res = 0, leftEven = 0, leftOdd = 0;
for (let i = 0; i < n; ++i) {
if (i % 2 === 0)
totalEven -= nums[i];
else
totalOdd -= nums[i];
if (leftEven + totalOdd === leftOdd + totalEven)
res++;
if (i % 2 === 0)
leftEven += nums[i];
else
leftOdd += nums[i];
}
return res;
};
Given an integer array nums
, you are to remove exactly one element from the array. After removal, an array is considered fair if the sum of elements at even indices is equal to the sum of elements at odd indices. Your task is to count how many different indices you can remove so that the resulting array is fair.
At first glance, you might consider removing each element one by one and checking if the resulting array is fair by recalculating the sums at even and odd indices. However, this brute-force approach would require recalculating sums for each removal, leading to a time complexity of O(n2), which is inefficient for larger arrays.
To optimize, we need a way to quickly compute the new sums after removing an element without traversing the entire array each time. The key insight is to use prefix sums, separating the sums for even and odd indices. By maintaining running totals for both sides of the removal point, we can efficiently compute the required sums in constant time for each index.
This approach reduces redundant computation and leverages the predictable shifting of indices after removal.
Let's break down the optimized algorithm step by step:
total_even
) and at odd indices (total_odd
).i
, simulate removing nums[i]
.left_even
and left_odd
, representing the sum of even and odd indices to the left of i
.total_even
or total_odd
by subtracting nums[i]
(since it's being "removed").nums[i]
is removed, elements after i
shift left, flipping their parity (even/odd index status).left_even
+ total_odd
(since the original odd indices after i
become even indices).left_odd
+ total_even
(since the original even indices after i
become odd indices).nums[i]
to left_even
or left_odd
for the next iteration, depending on the parity of i
.This method is efficient because it computes the fairness check in constant time for each index, using only a few variables and a single pass through the array.
Let's use the input nums = [2, 1, 6, 4]
as an example.
The only fair removal is at index 1. The function would return 1
.
The optimized approach is highly efficient and suitable even for large input arrays.
The key to solving the "Ways to Make a Fair Array" problem efficiently is recognizing how index shifting affects parity after removal. By maintaining running totals for even and odd indices and carefully updating them as elements are "removed," we can check fairness in constant time per index. This approach avoids unnecessary recomputation and leverages prefix sums for a clean and optimal O(n) solution.
The solution is elegant because it transforms a seemingly complex parity-shifting problem into a simple running sum calculation, highlighting the power of prefix sums and careful index management.