This community-built FAQ covers the “Max Product Finder” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Max Product Finder

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

def max_prod_for_sorted(arr, k):
res = 1
for el in arr[:k]:
res *= el
return res
def max_product_finder_k(arr, k):
bestres = float("-inf")
pos_for_odd = sorted([x for x in arr if x >= 0])
pos_for_even = pos_for_odd[::-1]
neg_for_even = sorted([x for x in arr if x < 0])
neg_for_odd = neg_for_even[::-1]
for i in range(k+1):
if len(neg_for_odd) < i or len(pos_for_odd) < k-i: continue
if i%2:
neg = neg_for_odd
pos = pos_for_odd
else:
neg = neg_for_even
pos = pos_for_even
res = max_prod_for_sorted(neg, i) * max_prod_for_sorted(pos, k-i)
bestres = max(bestres, res)
return bestres
print(max_product_finder_k([-1,-2,-3, -4], 1))

from itertools import combinations
def max_product_finder_k(arr, k):
res = []
C = combinations(arr, k)
for i in C:
n = 1
for j in i:
n *= j
res.append(n)
return max(res)
print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))

theres definitely easier solutions out there but i was trying not to use a library and also avoid going into permutations so I think it works quite well for that purpose.

def max_product_finder_k(arr, k):
def recur(arr,k,product):
if k == 0 or len(arr) == 0:
return product
return max([ recur([x for x in arr if x !=rem],k-1,product*rem) for rem in arr])
return recur(arr, k, 1)
print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))

using brute force recursion where
(base case) return accumulated product when

run out of k

no element to choose

(recursive case) find the max product of each element when it is removed from the array

eg max of key 1:[1,2,3] → max of key
{
3:[1,2]
2:[1,3]
1:[2,3]
}

import math
import numpy
def max_product_finder_k(arr, k):
if k==0:
return 0
else:
x=len(arr)-1
while x>0:
for i in range(0,x):
if math.fabs(arr[i])<math.fabs(arr[i+1]):
arr[i],arr[i+1]=arr[i+1],arr[i]
x-=1
l=arr[0:k]
if list(numpy.sign(l)).count(-1)%2==0:
s=1
for i in l:
s*=i
else:
l.sort(reverse=True)
for i in range(0,len(l)):
if l[i]<0:
for b in arr[k:]:
if b>=0:
l[i]=b
break
if list(numpy.sign(l)).count(-1)%2==0:
break
s=1
for i in l:
s*=i
return s
print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9],6))

Hi, could someone please explain why this is wrong. When I click test code, it says 4/5 tests passed. Thanks.

def max_product_finder_k(arr, k):
from itertools import combinations
from numpy import Inf
from functools import reduce
product = lambda x,y: x*y
max_ret = -Inf
for combo in list(combinations(arr,k)):
ret = reduce(product,combo)
if ret > max_ret:
max_ret = ret
return max_ret

from itertools import combinations
def product(arr):
result = 1
for num in arr:
result*=num
return result
def max_product_finder_k(arr, k):
comb = combinations(arr, k)
products = [product(nums) for nums in comb]
return max(products)
print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))