So here is the problem:
Input an array of positive integers and need to return sum of all elements in new array with conditions: if Array[i] > Array[j] then Array[i] = Array[i] - Array[j].
Example:
[1,2,3] => [1,2,1] => [1,1,1] => output: 3
My code:
def solution(a):
while max(a) != min(a):
u = max(a)
a[a.index(u)] = u - min(a)
return (a[0] * len(a))
But this code is very slow, how can I refactor it for better performance ?
sorted? Because it is a bit funny that you are usingmaxandmin, but notsorted.min,maxandindexareO(n), this is what makes the code slow.