Sherlock and Array

  • + 4 comments

    I had a similar idea, but yours is way better. Helped me figure out why mine was so slow:

    def solve(a):
        # Complete this function
        if len(a) == 1:
            return "YES"
        for i in range(1, int(len(a)+1/2)):
            left = sum(a[:i])
            right = sum(a[i+1:])
            if left == right:
                return "YES"
                break
        else:
            return "NO"
    

    Instead of computing the sum each time, just subtract. Way more efficient. Thank you for sharing.