#include
#include<bits/stdc++.h>
using namespace std;
map<int, pair<int,int>>mp;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int b[n];
for(int i=0;i<n;i++)
cin>>b[i];
int pos=0,neg=0;
for(int i=0;i<n;i++)
{
if(b[i]==-1)
neg++;
else
pos++;
}
for(int i=0;i<n;i++)
{
mp[i].first=pos;
mp[i].second=neg;
if(b[i]==1)
pos--;
else
neg--;
}
int totalp=0,totaln=0;
for(auto i:mp)
{
// cout<<i.first<<" "<<i.second.first<<" "<<i.second.second<<endl;
totalp+=i.second.first;
totaln+=i.second.second;
}
cout<<abs(totalp-totaln)<<endl;
mp.clear();
}
return 0;
}
Well this is my coded solution, for the question positive or negative subarrays in Start89 Codechef contest…Can Some1 please let me know where I’m going wrong…I’m going wrong in a few test cases…got AC in the rest of them.