Skip to content

Commit 4fee6df

Browse files
authored
Merge pull request #1619 from loczek/0724-find-pivot-index
Create: 0724-find-pivot-index.ts
2 parents 4236f85 + 317c3ad commit 4fee6df

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: typescript/0724-find-pivot-index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function pivotIndex(nums: number[]): number {
2+
let total = nums.reduce((a, b) => a + b);
3+
let leftSum = 0;
4+
5+
for (let i = 0; i < nums.length; i++) {
6+
const rightSum = total - nums[i] - leftSum;
7+
8+
if (leftSum === rightSum) return i;
9+
leftSum += nums[i];
10+
}
11+
12+
return -1;
13+
}

0 commit comments

Comments
 (0)