Skip to content

Commit 72f1999

Browse files
authored
Update 1664-ways-to-make-a-fair-array.js
1 parent c401e5c commit 72f1999

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

1664-ways-to-make-a-fair-array.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const waysToMakeFair = function(nums) {
6+
const n = nums.length, right = Array(2).fill(0), left = Array(2).fill(0)
7+
let res = 0
8+
for(let i = 0; i < n; i++) right[i % 2] += nums[i]
9+
for(let i = 0; i < n; i++) {
10+
right[i % 2] -= nums[i]
11+
if(left[0] + right[1] === left[1] + right[0]) res++
12+
left[i % 2] += nums[i]
13+
}
14+
return res
15+
};
16+
17+
// another
18+
119
/**
220
* @param {number[]} nums
321
* @return {number}

0 commit comments

Comments
 (0)