-3

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)

1 Answer 1

0
sum(a) # if possible store the value of this outside loop as it is of O(n) complexity

for j in range(0,i): # keep a variable to calculate sum till i instead of looping over.

    sum1+=a[j]

def abc():
    a=[2]
    if len(a)==1:
        return ("yes",0)
    tsum=sum(a) #total sum
    psum=0
    for i in range(1,len(a)):
        psum=psum+a[i-1]     # temp var
        #print psum;
        if psum==tsum-a[i]-psum:
            return ("yes",i)
    return "no"
print abc()
8
  • did that but it does not pass that test case because of time out. Sep 1, 2017 at 11:22
  • I did not understand when you said # keep a variable to calculate sum till i instead of looping over. Sep 1, 2017 at 11:34
  • Yes it did work...thanks.So that inner loop was causing all that problem...silly me. Sep 1, 2017 at 11:54
  • Sorry but I donot know how to edit my answer as a code.Thing is I edited my code with a different logic to copy that first portion of array to another array and find the sum.But I still have timeout errors.How do I fix this.def abc(a): total=sum(a) if len(a)==1: return ("yes",0) for i in range(1,len(a)): a1=a[0:i] sum1=sum(a1) if sum1==(total-sum1)-a[i]: return "YES" return "NO" Sep 4, 2017 at 7:27
  • Please look into this Sep 4, 2017 at 7:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.