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 FormatThe 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 FormatFor 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)
HackerRank, Sherlock and Array
python;programming challenge;time limit exceeded
General commentsYou should follow PEP8, the coding standard for python. This means using underscore_names for variables instead of camelCase.i is a bad generic name, except when iterating explicitly over integers. Maybe use for arr in A.I would use more descriptive variable names. Instead of A use maybe arrays?Your input could be more succinct:for i in range(T):    N = int(input)    A.append(map(int, input().split()))This will store a map object in A, instead of a list. But since that is iterable we won't have any problems.If the input was in one line with the first element being the length of the array and the rest of the line being the array, it would have been even easier:for i in range(T):    N, *arr = map(int, input().split())    A.append(arr)This uses a nice feature of python3. N will take the first element of an iterable and arr will take all the (possible) rest. You can even specify variables at the end. Try these cases out to get a feel for it:a, *b = []            # ValueError: not enough values to unpack (expected at least 1, got 0)a, *b = [0]           # a = 0, b = []a, *b = [0,1]         # a = 0, b = [1]a, *b = [0,1,2]       # a = 0, b = [1,2]a, *b, c = [0]        # ValueError: not enough values to unpack (expected at least 2, got 1)a, *b, c = [0,1]      # a = 0, b = [], c = 1a, *b, c = [0,1,2]    # a = 0, b = [1], c = 2PerformanceInstead of always calculating all sums, you could store the pre_ and post_sum and add/subtract the current element. You should also stop after having found one occurrence.for array in A:    found = False    pre_sum, post_sum = 0, sum(array)    for element in array:        post_sum -= element        if(pre_sum == post_sum):            found = True            break        pre_sum += element    print(YESif found else NO)I'm not exactly sure, but I think there is a small performance difference between these two, for large arr:arr = list(map(int, arr))# andarr = [int(x) for x in arr]