pranay_teja
11/16/2018 - 2:18 PM

LeetCode-FindPivotIndex

#include<bits/stdc++.h>
using namespace std;


int pivotIndex(vector<int> a) {
    int sumL=0,sumR=0;
    for(int i=0;i<a.size();i++){
        sumR+=a[i];
    }
    for(int i=0;i<a.size();i++){
        sumR-=a[i];
        // cout<<sumL<<" "<<sumR<<endl;
        if(sumL==sumR){
            return i;
        }
        sumL+=a[i];
    }
    return -1;
}

int main(){
    // freopen("ip.txt","r",stdin);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        cout<<pivotIndex(a)<<endl;
    }
    return 0;
}