Loading

LeetCode problem#724

Find Pivot Index

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

19 lines
1class Solution {
2    public int pivotIndex(int[] nums) {
3        int totalSum = 0;
4        int leftSum = 0;
5
6        for (int i = 0; i < nums.length; i++) {
7            totalSum += nums[i];
8        }
9
10        for (int i = 0; i < nums.length; i++) {
11            if (leftSum == totalSum - nums[i] - leftSum) {
12                return i;
13            }
14            leftSum += nums[i];
15        }
16
17        return -1;
18    }
19}

Want a walkthrough of this Java solution?

Get a step-by-step explanation of the idea behind the code.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.