Maximum product subset of an array
Given an array a, we have to find the maximum product possible with the subset of elements present in the array. The maximum product can be a single element also.
Examples:
Input: a[] = { -1, -1, -2, 4, 3 }
Output: 24
Explanation : Maximum product will be ( -2 * -1 * 4 * 3 ) = 24
Input: a[] = { -1, 0 }
Output: 0
Explanation: 0(single element) is maximum product possible
Maximum product subset of an array using the count of positive and negative elements
- The idea is to count the occurrence of positive and negative elements
- If there are even number of negative numbers and no zeros, the result is simply the product of all
- If there are odd number of negative numbers and no zeros,then the result is the product of all except the negative integer with the least absolute value.
- If there are zeros, the result is the product of all except these zeros with one exceptional case. The exceptional case is when there is one negative number and all other elements are 0. In this case, the result is 0.
Follow the steps mentioned below to implement the idea:
- Create 3 variables count_neg , count_zero and prod , to store the occurrence of negative elements, zeros, and the product of the subset.
- If count_neg is even then return prod.
- If count_neg is odd then divide the smallest absolute negative element from prod and return prod.
- If the array is filled with zeroes and negative elements then return 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProductSubset( int a[], int n)
{
if (n == 1)
return a[0];
int max_neg = INT_MIN;
int count_neg = 0, count_zero = 0;
int 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]);
}
prod = prod * a[i];
}
if (count_zero == n)
return 0;
if (count_neg & 1) {
if (count_neg == 1 &&
count_zero > 0 &&
count_zero + count_neg == n)
return 0;
prod = prod / max_neg;
}
return prod;
}
int main()
{
int a[] = { -1, -1, -2, 4, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << maxProductSubset(a, n);
return 0;
}
|
C
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
int maxProductSubset( int a[], int n)
{
if (n == 1)
return a[0];
int max_neg = -100000009;
int count_neg = 0, count_zero = 0;
int prod = 1;
for ( int i = 0; i < n; i++) {
if (a[i] == 0) {
count_zero++;
continue ;
}
if (a[i] < 0) {
count_neg++;
max_neg = fmax(max_neg, a[i]);
}
prod = prod * a[i];
}
if (count_zero == n)
return 0;
if (count_neg & 1) {
if (count_neg == 1 &&
count_zero > 0 &&
count_zero + count_neg == n)
return 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" ,maxProductSubset(a, n));
return 0;
}
|
Java
class GFG {
static int maxProductSubset( int a[], int n) {
if (n == 1 ) {
return a[ 0 ];
}
int max_neg = Integer.MIN_VALUE;
int count_neg = 0 , count_zero = 0 ;
int prod = 1 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == 0 ) {
count_zero++;
continue ;
}
if (a[i] < 0 ) {
count_neg++;
max_neg = Math.max(max_neg, a[i]);
}
prod = prod * a[i];
}
if (count_zero == n) {
return 0 ;
}
if (count_neg % 2 == 1 ) {
if (count_neg == 1
&& count_zero > 0
&& count_zero + count_neg == n) {
return 0 ;
}
prod = prod / max_neg;
}
return prod;
}
public static void main(String[] args) {
int a[] = {- 1 , - 1 , - 2 , 4 , 3 };
int n = a.length;
System.out.println(maxProductSubset(a, n));
}
}
|
Python3
def maxProductSubset(a, n):
if n = = 1 :
return a[ 0 ]
max_neg = - 999999999999
count_neg = 0
count_zero = 0
prod = 1
for i in range (n):
if a[i] = = 0 :
count_zero + = 1
continue
if a[i] < 0 :
count_neg + = 1
max_neg = max (max_neg, a[i])
prod = prod * a[i]
if count_zero = = n:
return 0
if count_neg & 1 :
if (count_neg = = 1 and count_zero > 0 and
count_zero + count_neg = = n):
return 0
prod = int (prod / max_neg)
return prod
if __name__ = = '__main__' :
a = [ - 1 , - 1 , - 2 , 4 , 3 ]
n = len (a)
print (maxProductSubset(a, n))
|
C#
using System;
class GFG
{
static int maxProductSubset( int []a,
int n)
{
if (n == 1)
{
return a[0];
}
int max_neg = int .MinValue;
int count_neg = 0, count_zero = 0;
int prod = 1;
for ( int i = 0; i < n; i++)
{
if (a[i] == 0)
{
count_zero++;
continue ;
}
if (a[i] < 0)
{
count_neg++;
max_neg = Math.Max(max_neg, a[i]);
}
prod = prod * a[i];
}
if (count_zero == n)
{
return 0;
}
if (count_neg % 2 == 1)
{
if (count_neg == 1 && count_zero > 0 &&
count_zero + count_neg == n)
{
return 0;
}
prod = prod / max_neg;
}
return prod;
}
public static void Main()
{
int []a = {-1, -1, -2, 4, 3};
int n = a.Length;
Console.Write(maxProductSubset(a, n));
}
}
|
PHP
<?php
function maxProductSubset( $a , $n )
{
if ( $n == 1)
return $a [0];
$max_neg = PHP_INT_MIN;
$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 ]);
}
$prod = $prod * $a [ $i ];
}
if ( $count_zero == $n )
return 0;
if ( $count_neg & 1)
{
if ( $count_neg == 1 &&
$count_zero > 0 &&
$count_zero + $count_neg == $n )
return 0;
$prod = $prod / $max_neg ;
}
return $prod ;
}
$a = array (-1, -1, -2, 4, 3 );
$n = sizeof( $a );
echo maxProductSubset( $a , $n );
?>
|
Javascript
<script>
function maxProductSubset(a, n)
{
if (n == 1)
return a[0];
let max_neg = Number.MIN_SAFE_INTEGER;
let count_neg = 0; count_zero = 0;
let prod = 1;
for (let i = 0; i < n; i++)
{
if (a[i] == 0)
{
count_zero++;
continue ;
}
if (a[i] < 0)
{
count_neg++;
max_neg = Math.max(max_neg, a[i]);
}
prod = prod * a[i];
}
if (count_zero == n)
return 0;
if (count_neg & 1)
{
if (count_neg == 1 &&
count_zero > 0 &&
count_zero + count_neg == n)
return 0;
prod = prod / max_neg;
}
return prod;
}
let a = [-1, -1, -2, 4, 3 ];
let n = a.length;
document.write(maxProductSubset(a, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
22 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...