Equal Sum

PROBLEM :

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.


Input:

 The first line contains T, the number of test cases. For each test case, the first line contains N, the number of elements in the array A. The
second line for each test case contains N space-separated integers, denoting the array A.


Output:

For each test case print YES 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; otherwise print NO in a separate line.


Constraints:

1= T = 30
1= N =100000
1= Ai = 2×10000
1= i =N


Example:

Input
1
4
1 2 3 3

Output:
YES

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Equal_Sum(long long [],long long ) ;

int main()
 {
int t ;
long long a[100000],no,i ;
cin>>t ;
while(t--)
{
   cin>>no ;

   for(i=0;i<no;i++)
       cin>>a[i] ;
     
   no=Equal_Sum(a,no) ;

   if(no)
       cout<<"YES" ;
   else
       cout<<"NO" ;
     
   cout<<endl ;
}
return 0;
}

int Equal_Sum(long long arr[],long long no)
{
    long long i,total,sum;
   
    if(no==1)
        return 1 ;
    if(no==2)
        return 0 ;
       
    total=0 ;
    for(i=0;i<no;i++)
        total=total+arr[i] ;
       
    sum=0 ;
    for(i=0;i<no-1;i++)
    {
        sum=sum+arr[i] ;
        if(sum==total-sum-arr[i+1])
            return 1 ;
    }
    return 0 ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )