Posted on

Description

Submission

class Solution {
    int presumEven[100005];
    int presumOdd[100005];
public:
    int waysToMakeFair(vector<int>& nums) {
        
        int n = nums.size();
        if(nums.size() == 1) return 1;
        if(nums.size() == 2) return 0;
        
        presumEven[0] = presumEven[1] = nums[0];
        presumOdd[0] = 0;
        presumOdd[1] = nums[1];
        
        for(int i = 1; i < n; ++i) {
            if(i % 2 == 1) {
                presumOdd[i] = presumOdd[i-1] + nums[i];
                presumEven[i] = presumEven[i-1];
                
            } else {
                presumOdd[i] = presumOdd[i-1];
                presumEven[i] += presumEven[i-1] + nums[i];
            }
        }
        
//         for(int i = 0; i < n; ++i) {
//             cout << presumOdd[i] << " " << presumEven[i] << endl;
//         }
//         cout << endl;
        
        int ret = 0;
        if(presumEven[n-1] - nums[0] == presumOdd[n-1]) ret++;
        for(int i = 1; i < n; ++i) {
            // removing the current index
            int oddSum = presumOdd[i-1] + (presumEven[n-1] - presumEven[i]);
            int evenSum = presumEven[i-1] + (presumOdd[n-1] - presumOdd[i]);
            // cout << i << ": " << oddSum << " " << evenSum << endl;
            if(oddSum == evenSum) ret++;
        }
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *