<p>Given an array of integers <code>nums</code>, calculate the <strong>pivot index</strong> of this array.</p>

<p>The <strong>pivot index</strong> is the index where the sum of all the numbers <strong>strictly</strong> to the left of the index is equal to the sum of all the numbers <strong>strictly</strong> to the index&#39;s right.</p>

<p>If the index is on the left edge of the array, then the left sum is <code>0</code> because there are no elements to the left. This also applies to the right edge of the array.</p>

<p>Return <em>the <strong>leftmost pivot index</strong></em>. If no such index exists, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,7,3,6,5,6]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
The pivot index is 3.
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
Right sum = nums[4] + nums[5] = 5 + 6 = 11
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
There is no index that satisfies the conditions in the problem statement.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The pivot index is 0.
Left sum = 0 (no elements to the left of index 0)
Right sum = nums[1] + nums[2] = 1 + -1 = 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as&nbsp;1991:&nbsp;<a href="https://leetcode.com/problems/find-the-middle-index-in-array/" target="_blank">https://leetcode.com/problems/find-the-middle-index-in-array/</a></p>

class Solution {
public:
// int bruteForce (vector<int>& nums){
//      //  cout << lsum << rsum<<endl;
// int size = nums.size();
//      for(int i = 0;i<size;i+=1){
//            int lsum = 0;
//          int rsum = 0;
//             // Left sum
//             for(int j = 0;j<i;j+=1){
//                 lsum += nums[j];
//             }

//             // Right Sum
//             for(int j = i+1;j<size;j+=1){
//                 rsum += nums[j];
//             }

//             if(rsum == lsum){
//                 return i;
//             }

//      }   


//      return -1;
//     };
int optimal(vector<int>& nums){
    vector <int> lsum(nums.size(),0);
    vector <int> rsum(nums.size(),0);
    int size = nums.size();
    
    // Left Side sum
    for(int i = 1;i<size;i+=1){
        lsum[i] = lsum[i-1] + nums[i-1];
    }
    // Right side sum
    for(int i = size-2;i>=0;i--){
        rsum[i] = rsum[i+1] + nums[i+1];
    }

    for(int i = 0;i<size;i+=1){
        if(lsum[i] == rsum[i]){
            return i;
        }
    }

    return -1 ;
}


    int pivotIndex(vector<int>& nums) {
    //  return   bruteForce(nums);
     return   optimal(nums);
    };
};
36000