INTRODUCTION:
The minimum product subset of an array refers to a subset of elements from the array such that the product of the elements in the subset is minimized. To find the minimum product subset, various algorithms can be used, such as greedy algorithms, dynamic programming, and branch and bound. The choice of algorithm depends on the specific constraints and requirements of the problem.
- One common algorithm used to find the minimum product subset of an array is the greedy algorithm. The basic idea of this algorithm is to start with the first element of the array and add the next element to the subset only if it will result in a smaller product. The advantage of this algorithm is its simplicity and ease of implementation. However, the greedy algorithm may not always produce the optimal solution and can be very slow for large arrays.
- Another algorithm used for this problem is dynamic programming. The dynamic programming algorithm divides the problem into subproblems and solves each subproblem only once, using the solutions of smaller subproblems to find the solution for larger ones. This can lead to significant time and space savings. The advantage of dynamic programming is that it always provides the optimal solution, but it can be more complex to implement compared to the greedy algorithm.
- Branch and bound is another algorithm that can be used to find the minimum product subset of an array. This algorithm involves searching for a solution by branching into multiple possibilities and bounding the search to only consider valid solutions. The advantage of this algorithm is that it provides the optimal solution and can be faster than other algorithms for certain cases. However, it can also be more complex to implement and may require more time and space compared to other algorithms.
In conclusion, the choice of algorithm depends on the specific constraints and requirements of the problem, such as the size of the array, the required solution accuracy, and the available computational resources.
Given 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minProductSubset( int a[], int n)
{
if (n == 1)
return a[0];
int max_neg = INT_MIN, min_pos = INT_MAX, count_neg = 0,
count_zero = 0, prod = 1;
for ( int i = 0; i < n; i++) {
if (a[i] == 0) {
count_zero++;
continue ;
}
if (a[i] < 0) {
count_neg++;
max_neg = max(max_neg, a[i]);
}
if (a[i] > 0)
min_pos = min(min_pos, a[i]);
prod = prod * a[i];
}
if (count_zero == n || (count_neg == 0 && count_zero > 0))
return 0;
if (count_neg == 0)
return min_pos;
if (!(count_neg & 1) && count_neg != 0)
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;
}
|
C
#include <limits.h>
#include <stdio.h>
int max( int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
int min( int num1, int num2)
{
return (num1 > num2) ? num2 : num1;
}
int minProductSubset( int a[], int n)
{
if (n == 1)
return a[0];
int max_neg = INT_MIN, min_pos = INT_MAX, count_neg = 0,
count_zero = 0, prod = 1;
for ( int i = 0; i < n; i++) {
if (a[i] == 0) {
count_zero++;
continue ;
}
if (a[i] < 0) {
count_neg++;
max_neg = max(max_neg, a[i]);
}
if (a[i] > 0)
min_pos = min(min_pos, a[i]);
prod = prod * a[i];
}
if (count_zero == n || (count_neg == 0 && count_zero > 0))
return 0;
if (count_neg == 0)
return min_pos;
if (!(count_neg & 1) && count_neg != 0)
prod = prod / max_neg;
return prod;
}
int main()
{
int a[] = { -1, -1, -2, 4, 3 };
int n = sizeof (a) / sizeof (a[0]);
printf ( "%d" , minProductSubset(a, n));
return 0;
}
|
Java
class GFG {
static int minProductSubset( int a[], int n)
{
if (n == 1 )
return a[ 0 ];
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 (a[i] == 0 ) {
count_zero++;
continue ;
}
if (a[i] < 0 ) {
count_neg++;
negmax = Math.max(negmax, a[i]);
}
if (a[i] > 0 && a[i] < posmin)
posmin = a[i];
product *= a[i];
}
if (count_zero == n
|| (count_neg == 0 && count_zero > 0 ))
return 0 ;
if (count_neg == 0 )
return posmin;
if (count_neg % 2 == 0 && count_neg != 0 ) {
product = product / negmax;
}
return product;
}
public static void main(String[] args)
{
int a[] = { - 1 , - 1 , - 2 , 4 , 3 };
int n = 5 ;
System.out.println(minProductSubset(a, n));
}
}
|
Python3
def minProductSubset(a, n):
if (n = = 1 ):
return a[ 0 ]
max_neg = float ( '-inf' )
min_pos = float ( 'inf' )
count_neg = 0
count_zero = 0
prod = 1
for i in range ( 0 , n):
if (a[i] = = 0 ):
count_zero = count_zero + 1
continue
if (a[i] < 0 ):
count_neg = count_neg + 1
max_neg = max (max_neg, a[i])
if (a[i] > 0 ):
min_pos = min (min_pos, a[i])
prod = prod * a[i]
if (count_zero = = n or (count_neg = = 0
and count_zero > 0 )):
return 0
if (count_neg = = 0 ):
return min_pos
if ((count_neg & 1 ) = = 0 and
count_neg ! = 0 ):
prod = int (prod / max_neg)
return prod
a = [ - 1 , - 1 , - 2 , 4 , 3 ]
n = len (a)
print (minProductSubset(a, n))
|
C#
using System;
public class GFG {
static int minProductSubset( int [] a, int n)
{
if (n == 1)
return a[0];
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 (a[i] == 0) {
count_zero++;
continue ;
}
if (a[i] < 0) {
count_neg++;
negmax = Math.Max(negmax, a[i]);
}
if (a[i] > 0 && a[i] < posmin) {
posmin = a[i];
}
product *= a[i];
}
if (count_zero == n
|| (count_neg == 0 && count_zero > 0))
return 0;
if (count_neg == 0)
return posmin;
if (count_neg % 2 == 0 && count_neg != 0) {
product = product / negmax;
}
return product;
}
public static void Main()
{
int [] a = new int [] { -1, -1, -2, 4, 3 };
int n = 5;
Console.WriteLine(minProductSubset(a, n));
}
}
|
PHP
<?php
function minProductSubset( $a , $n )
{
if ( $n == 1)
return $a [0];
$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 ( $a [ $i ] == 0)
{
$count_zero ++;
continue ;
}
if ( $a [ $i ] < 0)
{
$count_neg ++;
$max_neg = max( $max_neg , $a [ $i ]);
}
if ( $a [ $i ] > 0)
$min_pos = min( $min_pos , $a [ $i ]);
$prod = $prod * $a [ $i ];
}
if ( $count_zero == $n ||
( $count_neg == 0 &&
$count_zero > 0))
return 0;
if ( $count_neg == 0)
return $min_pos ;
if (!( $count_neg & 1) &&
$count_neg != 0)
{
$prod = $prod / $max_neg ;
}
return $prod ;
}
$a = array ( -1, -1, -2, 4, 3 );
$n = sizeof( $a );
echo (minProductSubset( $a , $n ));
?>
|
Javascript
<script>
function minProductSubset(a, n)
{
if (n == 1)
return a[0];
let negmax = Number.MAX_VALUE;
let posmin = Number.NEGATIVE_INFINITY;
let count_neg = 0, count_zero = 0;
let product = 1;
for (let i = 0; i < n; i++)
{
if (a[i] == 0)
{
count_zero++;
continue ;
}
if (a[i] < 0)
{
count_neg++;
negmax = Math.max(negmax, a[i]);
}
if (a[i] > 0 && a[i] < posmin)
{
posmin = a[i];
}
product *= a[i];
}
if (count_zero == n || (count_neg == 0 &&
count_zero > 0))
return 0;
if (count_neg == 0)
return posmin;
if (count_neg % 2 == 0 && count_neg != 0)
{
product = parseInt(product / negmax, 10);
}
return product;
}
let a = [ -1, -1, -2, 4, 3 ];
let n = 5;
document.write(minProductSubset(a, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Feb, 2023
Like Article
Save Article