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

Source


    int pivotIndex(vector<int>& nums) {
        int pivot = -1;
        int right_sum = 0;
        for (const auto &n : nums) {
            right_sum += n;
        }
        int left_sum = 0;
        for (int i=0; i<nums.size(); ++i) {
            right_sum -= nums[i];
            if (right_sum == left_sum) {
                pivot = i;
                break;
            }
            left_sum += nums[i];
        }
        return pivot;
    }

GitHub

FindPivotIndex