1333C. Eugene and an array - Codeforces Solution C++

  Problem Link : 1333C. Eugene and an array 


✅ C++ Solution :

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

#define ll long long int

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
	cin>>n;
	ll arr[n+1];
	for(ll i=1;i<=n;i++)
		cin>>arr[i];

	ll j=1;
	map<ll,ll> mp;
	ll curr=0;
	ll ans=0;
	mp[0]=0;
	for(ll i=1;i<=n;i++)
	{
		curr+=arr[i];
		if(arr[i]==0)
			j=i+1;
		else
		{
	        auto it=mp.find(curr);
			if(it!=mp.end())
				j=max(j,it->second+2);
			ans+=i-j+1;
		}
		mp[curr]=i;
	}

	cout<<ans;
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!