Python Forum
Find the maximum multiplication
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the maximum multiplication
#1
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:
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)
Reply


Messages In This Thread
Find the maximum multiplication - by ercv - Nov-30-2020, 07:51 AM
RE: Find the maximum multiplication - by DeaD_EyE - Nov-30-2020, 10:30 AM
RE: Find the maximum multiplication - by DPaul - Nov-30-2020, 10:54 AM
RE: Find the maximum multiplication - by DeaD_EyE - Nov-30-2020, 11:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  If statements and multiplication elroberto 1 1,753 Jun-22-2022, 05:35 PM
Last Post: deanhystad
  Find the maximum ercv 0 1,436 Nov-18-2020, 12:20 PM
Last Post: ercv
  Multiplication Table Homework mcnhscc39 6 4,691 Nov-25-2018, 04:01 PM
Last Post: mcnhscc39
  creating a 3x3 multiplication table aditvaddi 1 3,653 Jun-18-2018, 06:05 AM
Last Post: volcano63
  Multiplication Table funnybone04 4 5,824 Apr-08-2018, 03:03 AM
Last Post: nilamo
  Nested Loop multiplication table SushiRolz 3 10,277 Feb-28-2018, 04:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020