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
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Array
You are viewing a single comment's thread. Return to all comments →
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