We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

In python, sum(n) function has O(n) time. Because each loop time, you calculate again "left list" and "rigth list", so I can say that your program has O(n^2) time.

To solve this, you should calculate sum_left and sum_right at 0 position outsite your loop, then each loop time, you to do:
sum_left += arr[i-1]
sum_right -= arr[i]
Then compare sum_left and sum_right. With solution, your program will only take O(n) time

## Sherlock and Array

You are viewing a single comment's thread. Return to all comments →

Test case 3 and 4 are timed out for me. My code is in python as bellow. Do some one know what is wrong here?

In python, sum(n) function has O(n) time. Because each loop time, you calculate again "left list" and "rigth list", so I can say that your program has O(n^2) time.

To solve this, you should calculate sum_left and sum_right at 0 position outsite your loop, then each loop time, you to do: sum_left += arr[i-1] sum_right -= arr[i] Then compare sum_left and sum_right. With solution, your program will only take O(n) time