1664.cpp (581B)
1 class Solution { 2 public: 3 int waysToMakeFair(const vector<int> &nums) { 4 const int n = nums.size(); 5 int sodd = 0, seven = 0; 6 for (int i = n - 1; i >= 0; i--) 7 (i % 2 ? sodd : seven) += nums[i]; 8 9 int res = 0; 10 for (int i = 0, odd = 0, even = 0; i < n; i++) { 11 const int codd = odd + seven - even + (i % 2 ? nums[i] : 0); 12 const int ceven = even + sodd - odd + (i % 2 ? 0 : nums[i]); 13 if (codd == ceven) res++; 14 (i % 2 ? odd : even) += nums[i]; 15 } 16 17 return res; 18 } 19 };