Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the subset of a set of integers that has the maximum product

Let A be a non-empty set of integers. Write a function find that outputs a non-empty subset of A that has the maximum product. For example, find([-1, -2, -3, 0, 2]) = 12 = (-2)*(-3)*2

Here's what I think: divide the list into a list of positive integers and a list of negative integers:

  1. If we have an even number of negative integers, multiply everything in both list and we have the answer.
  2. If we have an odd number of negative integers, find the largest and remove it from the list. Then multiply everything in both lists.
  3. If the list has only one element, return this element.

Here's my code in Python:

def find(xs):
    neg_int = []
    pos_int = []
    if len(xs) == 1:
        return str(xs[0])
    for i in xs:
        if i < 0:
            neg_int.append(i)
        elif i > 0:
            pos_int.append(i)
    if len(neg_int) == 1 and len(pos_int) == 0 and 0 in xs:
        return str(0)
    if len(neg_int) == len(pos_int) == 0:
        return str(0)
    max = 1
    if len(pos_int) > 0:
        for x in pos_int:
            max=x*max
    if len(neg_int) % 2 == 1:
        max_neg = neg_int[0]
        for j in neg_int:
            if j > max_neg:
                max_neg = j
        neg_int.remove(max_neg)
    for k in neg_int:
        max = k*max
    return str(max)

Am I missing anything? P.S. This is a problem from Google's foobar challenge, I am apparently missing one case but I don't know which.

Now here's actual problem: enter image description here

like image 929
user112358 Avatar asked Oct 04 '16 05:10

user112358


People also ask

How do you find the maximum product of a number?

(Generally, for two numbers whose sum is n, the largest product is given by (n/2)2, for three numbers whose sum is n, the largest product is given by (n/3)3... The nearest integer to (n/e) where e = 2.718 is the number of numbers which will give the maximum product.)

How to find a Subarray of maximum product?

The maximum product subarray is the product of all the integers if the array only contains positive numbers. The most effective way to solve the problem is through dynamic programming . O(N) is the time complexity, and O ( 1 ) O(1) O(1) is the space complexity .


2 Answers

from functools import reduce
from operator import mul

def find(array):
    negative = []
    positive = []
    zero = None
    removed = None

    def string_product(iterable):
        return str(reduce(mul, iterable, 1))

    for number in array:
        if number < 0:
            negative.append(number)
        elif number > 0:
            positive.append(number)
        else:
            zero = str(number)

    if negative:
        if len(negative) % 2 == 0:
            return string_product(negative + positive)

        removed = max(negative)

        negative.remove(removed)

        if negative:
            return string_product(negative + positive)

    if positive:
        return string_product(positive)

    return zero or str(removed)
like image 113
cdlane Avatar answered Sep 25 '22 16:09

cdlane


You can simplify this problem with reduce (in functools in Py3)

import functools as ft
from operator import mul

def find(ns):
    if len(ns) == 1 or len(ns) == 2 and 0 in ns:
        return str(max(ns))
    pos = filter(lambda x: x > 0, ns)
    negs = sorted(filter(lambda x: x < 0, ns))
    return str(ft.reduce(mul, negs[:-1 if len(negs)%2 else None], 1) * ft.reduce(mul, pos, 1))

>>> find([-1, -2, -3, 0, 2])
'12'
>>> find([-3, 0])
'0'
>>> find([-1])
'-1'
>>> find([])
'1'
like image 29
AChampion Avatar answered Sep 24 '22 16:09

AChampion