To find the pivot index
#include <bits/stdc++.h>
using namespace std;
void pivotIndex(vector<int>&nums){
int totalSum=0;
// find the totalSum of the vector
for(auto num:nums){
totalSum=totalSum+num;
}
int lsum=0;
for(int i=0;i<nums.size();i++){
int rsum=totalSum-lsum-nums[i]; //we are considering nums[i] as a pivot index
if(lsum==rsum){
cout<<i<<endl;
return; //this is used to came out of the loop once you get the pivot index
// return i;
}
lsum += nums[i]; //if lsum and rsum are not then add considering element nums[i] in the lsum
}
// return -1;
cout<<-1<<endl; //if we reach here and did not get the answer then return -1;
}
int main()
{
vector<int>nums={1,7,3,6,5,6};
pivotIndex(nums);
return 0;
}