# algorithm – Find the highest product of three numbers in a list (Python)

I wrote the below as a solution to:

### Problem

Find the highest product of three numbers in a list

### Constraints

• Is the input a list of integers?
• Can we get negative inputs?
• Can there be duplicate entries in the input?
• Will there always be at least three integers?
• Can we assume the inputs are valid?
• Can we assume this fits memory?

### Solution

``````from functools import reduce
from typing import List, Set, Callable

class Solution(object):

def max_prod_three(self, array: List(int)) -> int:
if array is None:
raise TypeError("array cannot be None")
if len(array) < 3:
raise ValueError("array must have at least 3 elements")
pos: List(int) = (n for n in array if n >= 0)
neg: List(int) = (n for n in array if n < 0)
pos_len: int = len(pos)
neg_len: int = len(neg)

candidates: Set(int) = set()
mult: Callable((int, int), int) = lambda x,y:x*y

# possible combinations are:
# ---, --+, -+-, +--, -++, +-+, ++-, +++
# but order does not matter, so left with:
# --- -> -  : len(neg) >= 3, will never be the max if len(pos) > = 3, if len(pos) < 3,
#                            max will be with 3 lowest absolute values
# --+ -> +  : len(neg) >= 2, len(pos) >= 1, will be the max if  exists -- > any ++
# -++ -> -  : len(neg) >= 1, len(pos) >= 2, will never be the max if len(pos >= 3,
#                            if len(pos) == 2, max will be with lowest absolute value
# +++ -> +  : len(pos) >= 3, max is with 3 highest values

if neg_len >= 3 and pos_len < 3:
if neg_len >= 2 and pos_len >= 1:
if neg_len >= 1 and pos_len == 2:
if pos_len >= 3: