Minimum product subset of an array
Given an array a, we have to find minimum product possible with the subset of elements present in the array. The minimum product can be 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 product of every subset and return maximum 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.
C++
// CPP program to find maximum product of // a subset. #include <bits/stdc++.h> using namespace std; int minProductSubset( int a[], int 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 int max_neg = INT_MIN; int min_pos = INT_MAX; int count_neg = 0, count_zero = 0; int prod = 1; for ( int i = 0; i < n; i++) { // If number is 0, we don't // multiply it with product. if (a[i] == 0) { count_zero++; continue ; } // Count negatives and keep // track of maximum valued negative. if (a[i] < 0) { count_neg++; 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 || (count_neg == 0 && 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) && count_neg != 0) { // Otherwise result is product of // all non-zeros divided by maximum // valued negative. prod = prod / max_neg; } return prod; } int main() { int a[] = { -1, -1, -2, 4, 3 }; int n = sizeof (a) / sizeof (a[0]); cout << minProductSubset(a, n); return 0; } |
chevron_right
filter_none
Java
// Java program to find maximum product of // a subset. class GFG { static int minProductSubset( int a[], int 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 int negmax = Integer.MIN_VALUE; int posmin = Integer.MAX_VALUE; int count_neg = 0 , count_zero = 0 ; int product = 1 ; for ( int i = 0 ; i < n; i++) { // if number is zero,count it // but dont multiply if (a[i] == 0 ){ count_zero++; continue ; } // count the negetive numbers // and find the max negetive number if (a[i] < 0 ) { count_neg++; negmax = Math.max(negmax, a[i]); } // find the minimum positive number if (a[i] > 0 && a[i] < posmin) posmin = a[i]; product *= a[i]; } // if there are all zeroes // or zero is present but no // negetive number is present if (count_zero == n || (count_neg == 0 && count_zero > 0 )) return 0 ; // If there are all positive if (count_neg == 0 ) return posmin; // If there are even number except // zero of negative numbers if (count_neg % 2 == 0 && count_neg != 0 ) { // Otherwise result is product of // all non-zeros divided by maximum // valued negative. product = product / negmax; } return product; } // main function public static void main(String[] args) { int a[] = { - 1 , - 1 , - 2 , 4 , 3 }; int n = 5 ; System.out.println(minProductSubset(a, n)); } } // This code is contributed by Arnab Kundu. |
chevron_right
filter_none
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) |
chevron_right
filter_none
C#
// C# program to find maximum product of // a subset. using System; public class GFG { static int minProductSubset( int [] a, int 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 int negmax = int .MinValue; int posmin = int .MinValue; int count_neg = 0, count_zero = 0; int product = 1; for ( int i = 0; i < n; i++) { // if number is zero, count it // but dont multiply if (a[i] == 0) { count_zero++; continue ; } // count the negetive numbers // and find the max negetive number if (a[i] < 0) { count_neg++; negmax = Math.Max(negmax, a[i]); } // find the minimum positive number if (a[i] > 0 && a[i] < posmin) { posmin = a[i]; } product *= a[i]; } // if there are all zeroes // or zero is present but no // negetive number is present if (count_zero == n || (count_neg == 0 && count_zero > 0)) return 0; // If there are all positive if (count_neg == 0) return posmin; // If there are even number except // zero of negative numbers if (count_neg % 2 == 0 && count_neg != 0) { // Otherwise result is product of // all non-zeros divided by maximum // valued negative. product = product / negmax; } return product; } // main function public static void Main() { int [] a = new int [] { -1, -1, -2, 4, 3 }; int n = 5; Console.WriteLine(minProductSubset(a, n)); } } // This code is contributed by Ajit. |
chevron_right
filter_none
PHP
<?php // PHP program to find maximum // product of a subset. // Function to find maximum // product of a subset function 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 = PHP_INT_MIN; $min_pos = PHP_INT_MAX; $count_neg = 0; $count_zero = 0; $prod = 1; for ( $i = 0; $i < $n ; $i ++) { // If number is 0, we don't // multiply it with product. if ( $a [ $i ] == 0) { $count_zero ++; continue ; } // Count negatives and keep // track of maximum valued // negative. if ( $a [ $i ] < 0) { $count_neg ++; $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 || ( $count_neg == 0 && $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) && $count_neg != 0) { // Otherwise result is product of // all non-zeros divided by maximum // valued negative. $prod = $prod / $max_neg ; } return $prod ; } // Driver code $a = array ( -1, -1, -2, 4, 3 ); $n = sizeof( $a ); echo (minProductSubset( $a , $n )); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output:
-24
Time Complexity : O(n)
Auxiliary Space : O(1)
Recommended Posts:
- Maximum product subset of an array
- Find minimum value to assign all array elements so that array product becomes greater
- Product of maximum in first array and minimum in second
- Sum and Product of minimum and maximum element of an Array
- Minimum product pair an array of positive Integers
- Minimum element whose n-th power is greater than product of an array of size n
- Minimum product of k integers in an array of positive Integers
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find whether an array is subset of another array | Added Method 3
- Array with GCD of any of its subset belongs to the given array
- Largest divisible subset in array
- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Find the sum of maximum difference possible from all subset of a given array.
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Minimum sum of product of two arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.