DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on

724. Leetcode solution in CPP

class Solution {
public:
int pivotIndex(vector& nums) {
const int sum = accumulate(begin(nums), end(nums), 0);
int prefix = 0;

for (int i = 0; i < nums.size(); ++i) {
  if (prefix == sum - prefix - nums[i])
    return i;
  prefix += nums[i];
}

return -1;
Enter fullscreen mode Exit fullscreen mode

}
};
`

`

Discussion (0)