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
#2
https://docs.python.org/3.9/library/heap...q.nlargest

import heapq

data = (3, 5, 1, 7, 9, 0, 9, -3, 10)
nlargest = heapq.nlargest(3, data)
print(nlargest)
if nlargest == sorted(data, reverse=True)[:3]:
    print("Result is correct")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I wonder if there is a catch here.
Because if a list contains 2 negatives, eg. - 1000 and -2000,
their product is very large and positive.

Paul
buran likes this post
Reply
#4
Ah, I did not read it completely.

  1. Get 3 largest values
  2. Get 2 smallest values
  3. Compare the product of the largest values and smallest values

Is this right?

The function math.prod calculates the Product and was introduced with Python 3.8.


from heapq import nsmallest, nlargest
from math import prod

input_data = (3, 5, 1, 7, 9, 0, 9, -3, 10)
largest = nlargest(3, input_data)
smallest = nsmallest(2, input_data)

product_largest = prod(largest)
product_smallest = prod(smallest)

if  product_largest > product_smallest:
    print(
        f"Product of {largest} == {product_largest} "
        f"is bigger than the Product of {smallest} "
        f"== {product_smallest}"
    )
else:
    print(f"Product of {smallest} is bigger than the Product of {largest}")
Output:
Product of [10, 9, 9] == 810 is bigger than the Product of [-3, 0] == 0
print(prod(largest))
print(prod(smallest))
The result from nlargest: [10, 9, 9] # big values first
The result from nsmallest: [-3, 0] # small values first

Your data contains also negative values and a 0.
If there is one 0 in your data, the result of product is 0.
If you have an even number of negative values, the product will be positive.
An odd number of negative values will result in a negative result.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If statements and multiplication elroberto 1 1,719 Jun-22-2022, 05:35 PM
Last Post: deanhystad
  Find the maximum ercv 0 1,418 Nov-18-2020, 12:20 PM
Last Post: ercv
  Multiplication Table Homework mcnhscc39 6 4,628 Nov-25-2018, 04:01 PM
Last Post: mcnhscc39
  creating a 3x3 multiplication table aditvaddi 1 3,624 Jun-18-2018, 06:05 AM
Last Post: volcano63
  Multiplication Table funnybone04 4 5,789 Apr-08-2018, 03:03 AM
Last Post: nilamo
  Nested Loop multiplication table SushiRolz 3 10,250 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