Nov-30-2020, 07:51 AM
So you are given with the list of number, you should choose the three of them which multiplication is the greatest. The problem lies in the fact that the complexety of the code should be O(n). That's why the easiest solution (by sorting at first)is out. As for me the only way out is to find the first three maximum values(I understand how to find two numbers, but with the third I have some problems) and two minimum, and then compare their multiplication. Maybe there is easier solution?
Here is some tests:
Input:3 5 1 7 9 0 9 -3 10
Output:10 9 9
Input:-5 -30000 -12
Output:-5 -12 -30000
Here is the code for the same task, but for mutiplication of two numbers:
Here is some tests:
Input:3 5 1 7 9 0 9 -3 10
Output:10 9 9
Input:-5 -30000 -12
Output:-5 -12 -30000
Here is the code for the same task, but for mutiplication of two numbers:
a = list(map(int, input().split())) maxpos1, maxpos2 = a[0], a[1] maxneg1, maxneg2 = a[0], a[1] if len(a) == 2: print(min(maxpos1,maxpos2),max(maxpos1,maxpos2)) else: for i in range(2,len(a)): if a[i] >= maxpos1 and a[i] >= 0: maxpos2 = maxpos1 maxpos1 = a[i] elif maxpos2 < a[i] < maxpos1: maxpos2 = a[i] elif a[i] <= maxneg1 and a[i] < 0: maxneg2 = maxneg1 maxneg1 = a[i] elif maxneg1 < a[i] < maxneg2: maxneg2 = a[i] if maxneg1*maxneg2 > maxpos1*maxpos2: print(maxneg1, maxneg2) else: print(maxpos2, maxpos1)