HackerRank Sherlock and Array performance
05:40 20 Apr 2016

I did this small algorithm to the HackerRank Sherlock and Array test, but this gets timeouts in 2 test cases. These test cases creates huge lists and i couldn't see what is wrong in terms of performance.

This is the problem:

Watson gives Sherlock an array AA of length NN. Then he asks him to 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 ii, such that, AA1+A+A2...A...Ai-1 =A=Ai+1+A+Ai+2...A...AN.

Input Format The first line contains TT, the number of test cases. For each test case, the first line contains NN, the number of elements in the array AA. The second line for each test case contains NN space-separated integers, denoting the array AA.

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.

This is my code:

for turn in range(int(input())):
    lst_size = int(input())
    has_equal = False

    lst = list(map(int, input().split(" ")))

    if lst_size > 2:

        for i in range(lst_size):
            sumleft = sum(lst[:i])
            sumright = sum(lst[(i+1):])

            if sumleft == sumright:
                has_equal = True
                break

    if has_equal:
        print("YES")
    else:
        print("NO")
python performance python-3.x