Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions array-equal-split-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

const findIndex = (arr) => {
if (arr.length === 1 || arr.length === 2) {
console.log(`no index for arr with length ${arr.length}`);
return;
}

const sum = arr.reduce((acc, val) => acc + val, 0);

let leftSum = 0,
rightSum = sum - arr[0],
index = 1,
missingIndex = 0;

while (index < arr.length) {

// console.log(`${index} => ${leftSum}, ${rightSum}`);
if (leftSum === rightSum) {
// console.log('found index');
missingIndex = index - 1;
break;
}
rightSum = rightSum - arr[index];
leftSum = leftSum + arr[index - 1];
index = index + 1;
}


if (missingIndex === 0) {
console.log('no such index');
} else {
console.log(missingIndex);
}
};

findIndex([1]);
findIndex([1, 2]);
findIndex([1, 2, 3]); // no index
findIndex([2, 2, 2]); // index at 1
findIndex([1, 2, 3, 4, 10, 9, 5, 5, 4, 6]); // index at 5