Skip to content

Instantly share code, notes, and snippets.

@praneethreddymallupally
Created June 12, 2020 10:11
Show Gist options
  • Save praneethreddymallupally/26aa98864ed68394dfa0a0ef5e653cd9 to your computer and use it in GitHub Desktop.
Save praneethreddymallupally/26aa98864ed68394dfa0a0ef5e653cd9 to your computer and use it in GitHub Desktop.
Perfect-subarray
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<ll> a(n);
unordered_map<ll,ll> m;
m[0] = 1;
ll minSum = 0,sum = 0,ans = 0;
for(ll i = 0; i < n; i++)
{
cin >> a[i];
sum += a[i];
for(ll j=0;(sum-j*j)>=minSum;++j)
{
if(m.count(sum-j*j)==1)
ans+=m[sum-j*j];
}
minSum = min(minSum,sum);
m[sum]+=1;
}
cout<<ans<<"\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment