Python Program for Minimum product subset of an array Last Updated : 20 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array a, we have to find the minimum product possible with the subset of elements present in the array. The minimum product can be a single element also. Examples: Input : a[] = { -1, -1, -2, 4, 3 } Output : -24 Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24 Input : a[] = { -1, 0 } Output : -1 Explanation : -1(single element) is minimum product possible Input : a[] = { 0, 0, 0 } Output : 0 A simple solution is to generate all subsets, find the product of every subset and return the minimum product.A better solution is to use the below facts. If there are even number of negative numbers and no zeros, the result is the product of all except the largest valued negative number.If there are an odd number of negative numbers and no zeros, the result is simply the product of all.If there are zeros and positive, no negative, the result is 0. The exceptional case is when there is no negative number and all other elements positive then our result should be the first minimum positive number. Python3 # Python3 program to find maximum # product of a subset. # def to find maximum # product of a subset def minProductSubset(a, n): if (n == 1): return a[0] # Find count of negative numbers, # count of zeros, maximum valued # negative number, minimum valued # positive number and product # of non-zero numbers max_neg = float('-inf') min_pos = float('inf') count_neg = 0 count_zero = 0 prod = 1 for i in range(0, n): # If number is 0, we don't # multiply it with product. if (a[i] == 0): count_zero = count_zero + 1 continue # Count negatives and keep # track of maximum valued # negative. if (a[i] < 0): count_neg = count_neg + 1 max_neg = max(max_neg, a[i]) # Track minimum positive # number of array if (a[i] > 0): min_pos = min(min_pos, a[i]) prod = prod * a[i] # If there are all zeros # or no negative number # present if (count_zero == n or (count_neg == 0 and count_zero > 0)): return 0 # If there are all positive if (count_neg == 0): return min_pos # If there are even number of # negative numbers and count_neg # not 0 if ((count_neg & 1) == 0 and count_neg != 0): # Otherwise result is product of # all non-zeros divided by # maximum valued negative. prod = int(prod / max_neg) return prod # Driver code a = [-1, -1, -2, 4, 3] n = len(a) print(minProductSubset(a, n)) # This code is contributed by # Manish Shaw (manishshaw1) Output: -24 Time Complexity : O(n) Auxiliary Space : O(1) Please refer complete article on Minimum product subset of an array for more details! Comment More infoAdvertise with us Next Article Finding the k smallest values of a NumPy array K kartik Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python Program for Find minimum sum of factors of number Given a number, find minimum sum of its factors.Examples: Input : 12Output : 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3 = 2 + 2 + 3 = 7Therefore minimum sum is 7Inp 1 min read Program to print N minimum elements from list of integers Our task is to extract the smallest N elements from a list of integers.For example, if we have a list [5, 3, 8, 1, 2] and want the smallest 3 elements, the output should be [1, 2, 3]. Weâll explore different methods to achieve this.Using heapq.nsmallestThis method uses the nsmallest() function from 2 min read Finding the k smallest values of a NumPy array In this article, let us see how to find the k number of the smallest values from a NumPy array. Examples: Input: [1,3,5,2,4,6] k = 3 Output: [1,2,3] Method 1: Using np.sort() . Approach: Create a NumPy array.Determine the value of k.Sort the array in ascending order using the sort() method.Print th 2 min read numpy.minimum() in Python numpy.minimum() function is used to find the element-wise minimum of array elements. It compare two arrays and returns a new array containing the element-wise minima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned. 2 min read Find the maximum and minimum element in a NumPy array An array can be considered as a container with the same types of elements. Python has its array module named array. We can simply import the module and create our array. But this module has some of its drawbacks. The main disadvantage is we can't create a multidimensional array. And the data type mu 4 min read Python | Pandas Series.argmin() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.argmin() function returns the 3 min read Like