Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Find Pivot Index


Solution


    class Solution {
    public:
        int pivotIndex(vector<int>& nums) {
            int sum = 0, currSum = 0;
            for(int i = 0; i < nums.size(); i++)
                sum += nums[i];
            for(int i = 0; i < nums.size(); i++) {
                if(currSum == sum - currSum - nums[i])
                    return i;
                currSum += nums[i];
            }
            return -1;
        }
    };