So what I am doing is to find if it is possible to find a for which the sum of numbers on the right side and left side are equal for eg: 1 2 1 should return YES because sum of elements on either side of 2 are 1 but 1 1 2 1 should return false. My code works except for the following huge test case https://hr-testcases-us-east-1.s3.amazonaws.com/2490/input03.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1504259349&Signature=gdIq12uki3Nq8YeXKDAKdqJG9k0%3D&response-content-type=text%2Fplain
Input line is as followsThe first line contains , the number of test cases. For each test case, the first line contains , the number of elements in the array . The second line for each test case contains space-separated integers, denoting the array .
Logic of my program:if sum of first i elements =total-(sum of first i elements)-ith element
Code:
!/bin/python
import sys
def solve(a,n):
t=sum(a)
if len(a)==1:
return "YES"
for i in range(1,n-1):
sum1=0
sum2=0
j=i
for j in range(0,i):
sum1+=a[j]
# for j in range(i+1,n):
# sum2+=a[j]
# print str(sum1) + " " + str(sum2)
if sum1==(sum(a)-a[i])-sum1:
print str(sum1) + " " + str(sum2)
return "YES"
return "NO"
T = int(raw_input().strip()) for a0 in xrange(T):
n = int(raw_input().strip())
a = map(int, raw_input().strip().split(' '))
print "Iteration " + str(a0)
result = solve(a,n)
print(result)