3
int waysToMakeFair(vector<int> &nums) {4
// it looks a quite complicated problem but actually it is not5
// the main observation here is when a element is deleted the odd sum after6
// the element becomes the evensum and vice versa so we maintain two vectors8
vector<int> left(2, 0);9
vector<int> right(2, 0);11
// left[0],right[0] stores the sum of even indices elements to the left and12
// right side of the element respectively left[1] right[1] stores the sum of13
// odd indices elements to the left and right side of the element16
int ans = 0; // stores the result17
// first store the odd sum and even sum in right18
for (int i = 0; i < nums.size(); i++) {28
// now traverse through every element in the array and try to remove the29
// element and check does it makes a fair array30
for (int i = 0; i < nums.size(); i++) {31
// try to remove the element32
int currOdd = right[1];33
int currEven = right[0];35
// odd index , remove it from currOdd37
right[1] -= nums[i]; // since it would be no longer to the right39
// even index , remove it from currEven43
// now check whether the total oddSum and the evenSum in the array are44
// equal ? since we are deleting this element oddSum becomes evenSum and45
// evenSum becomes oddSum check leftOdd+rightOdd==rightEven+leftEven46
// left[0] is even sum to left of i47
// left[1] is the odd sum to left of i48
if (left[0] + currOdd == left[1] + currEven) ans++;49
// since we traverse to right add this value to the left array50
(i % 2) ? left[1] += nums[i] : left[0] += nums[i];