-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1664-ways-to-make-a-fair-array.js
49 lines (44 loc) · 1.37 KB
/
1664-ways-to-make-a-fair-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 1664. Ways to Make a Fair Array
* https://leetcode.com/problems/ways-to-make-a-fair-array/
* Difficulty: Medium
*
* You are given an integer array nums. You can choose exactly one index (0-indexed) and remove
* the element. Notice that the index of the elements may change after the removal.
*
* For example, if nums = [6,1,7,4,1]:
* - Choosing to remove index 1 results in nums = [6,7,4,1].
* - Choosing to remove index 2 results in nums = [6,1,4,1].
* - Choosing to remove index 4 results in nums = [6,1,7,4].
*
* An array is fair if the sum of the odd-indexed values equals the sum of the even-indexed values.
*
* Return the number of indices that you could choose such that after the removal, nums is fair.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var waysToMakeFair = function(nums) {
let evenSum = 0;
let oddSum = 0;
let result = 0;
for (let i = 0; i < nums.length; i++) {
if (i % 2 === 0) evenSum += nums[i];
else oddSum += nums[i];
}
let currentEven = 0;
let currentOdd = 0;
for (let i = 0; i < nums.length; i++) {
if (i % 2 === 0) {
evenSum -= nums[i];
if (currentEven + oddSum === currentOdd + evenSum) result++;
currentEven += nums[i];
} else {
oddSum -= nums[i];
if (currentEven + oddSum === currentOdd + evenSum) result++;
currentOdd += nums[i];
}
}
return result;
};