Equal sum geeks
Given an array A of length N. Determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of
the elements on its right. If there are no elements to the left/right, then the sum is considered to be zero.
Formally, find an i, such that, A1+A2...Ai-1 =Ai+1+Ai+2...AN.
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,flag=0;
cin>>n;
int a[n],sum=0,half=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
if(n==1)
{
cout<<"YES\n";
continue;
}
for(int i=1;i<n-1;i++)
{
half+=a[i-1];
if(sum-a[i]==2*half)
{
flag=1;
break;
}
}
if(flag)
cout<<"YES";
else
cout<<"NO";
cout<<'\n';
}
return 0;
}
the elements on its right. If there are no elements to the left/right, then the sum is considered to be zero.
Formally, find an i, such that, A1+A2...Ai-1 =Ai+1+Ai+2...AN.
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,flag=0;
cin>>n;
int a[n],sum=0,half=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
if(n==1)
{
cout<<"YES\n";
continue;
}
for(int i=1;i<n-1;i++)
{
half+=a[i-1];
if(sum-a[i]==2*half)
{
flag=1;
break;
}
}
if(flag)
cout<<"YES";
else
cout<<"NO";
cout<<'\n';
}
return 0;
}
0 Comments:
Post a Comment