Posted on

Description

Submission

class Solution {
    int presum[10005];
public:
    int pivotIndex(vector<int>& nums) {
        nums.insert(nums.begin(), 0);
        int n = nums.size();
        presum[0] = 0;
        for(int i = 1; i < nums.size(); ++i) {
            presum[i] = presum[i-1] + nums[i];
        }

        for(int i = 1; i < nums.size(); ++i) {
            if(presum[i-1] == presum[n-1] - presum[i]) return i-1;
        }

        return -1;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *