Leetcode 724 Solution

This article provides solution to leetcode question 724 (find-pivot-index)

https://leetcode.com/problems/find-pivot-index

Solution

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        int s = 0;
        for (int num: nums)
            s += num;

        int curr_sum = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (curr_sum * 2 + nums[i] == s)
                return i;

            curr_sum += nums[i];
        }

        return -1;
    }
};