DEV Community

Tshihab07
Tshihab07

Posted on

Hackerrank - Sherlock and Array Solution in Python

Time Complexity of the program O(n).
if the middle value is q and the sum is p then we can write the equation as follow

  1. p + q + p = sum [as the left and the right sum are same]
  2. 2p = sum - q

According to that Mathematical Logic The code can be written as follow:

def balancedSums(arr):
    # Write your code here
    left_sum = 0
    arr_sum = sum(arr)

    for i in arr:

        if (2 * left_sum) == arr_sum - i:
            return "YES"

        left_sum += i

    return "NO"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)