Here is the code I have written for a HackerRank challenge which takes multiple arrays, and attempts to determine if there exists an element in an 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.
Detailed problem statement:
Input Format
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 Format
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.
It works fine, but it's definitely not an optimal solution, as it fails two test cases (time outs occur). Could anybody provide any insights on optimising it further?
T = int(input())
N = []
A = []
for i in range(T):
#N.append(int(input()))
N = int(input())
arr = input().strip().split(' ')
arr = list(map(int, arr))
A.append(arr)
#print(A)
for i in A:
count=0
for j in range(len(i)):
preSum = sum(i[0:j])
postSum = sum(i[j+1:len(i)])
if(preSum == postSum):
count+=1
if(count>0):
print("YES")
else:
print("NO")