Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function makePSA(arr) {
- const PSA = new Array(arr.length + 1);
- let sum = 0;
- for (let i = 0; i <= arr.length; i++) {
- PSA[i] = sum;
- sum += arr[i];
- }
- return PSA;
- }
- function queryPSA(PSA, start, end) {
- return (PSA[end] - PSA[start]) || 0;
- }
- function findEvenIndex(arr) {
- const PSA = makePSA(arr);
- for (let index = 0; index <= arr.length; index++) {
- let leftSum = queryPSA(PSA, 0, index);
- let rightSum = queryPSA(PSA, index + 1, arr.length);
- console.log(index, leftSum, rightSum);
- if (leftSum === rightSum) {
- return index;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement