Maximum product subset of an array
Last Updated :
22 Aug, 2022
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++
// CPP program to find maximum product of
// a subset.
#include <bits/stdc++.h>
using namespace std;
int maxProductSubset(int a[], int n)
{
if (n == 1)
return a[0];
// Find count of negative numbers, count
// of zeros, negative number
// with least absolute value
// and product of non-zero numbers
int max_neg = INT_MIN;
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 negative number
// with least absolute value
if (a[i] < 0) {
count_neg++;
max_neg = max(max_neg, a[i]);
}
prod = prod * a[i];
}
// If there are all zeros
if (count_zero == n)
return 0;
// If there are odd number of
// negative numbers
if (count_neg & 1) {
// Exceptional case: There is only
// negative and all other are zeros
if (count_neg == 1 &&
count_zero > 0 &&
count_zero + count_neg == n)
return 0;
// Otherwise result is product of
// all non-zeros divided by
//negative number with
// least absolute value
prod = prod / max_neg;
}
return prod;
}
// Driver Code
int main()
{
int a[] = { -1, -1, -2, 4, 3 };
int n = sizeof(a) / sizeof(a[0]);
cout << maxProductSubset(a, n);
return 0;
}
C
// C program to find maximum product of
// a subset.
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
int maxProductSubset(int a[], int n)
{
if (n == 1)
return a[0];
// Find count of negative numbers, count
// of zeros, negative number
// with least absolute value
// and product of non-zero numbers
int max_neg = -100000009;
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 negative number
// with least absolute value
if (a[i] < 0) {
count_neg++;
max_neg = fmax(max_neg, a[i]);
}
prod = prod * a[i];
}
// If there are all zeros
if (count_zero == n)
return 0;
// If there are odd number of
// negative numbers
if (count_neg & 1) {
// Exceptional case: There is only
// negative and all other are zeros
if (count_neg == 1 &&
count_zero > 0 &&
count_zero + count_neg == n)
return 0;
// Otherwise result is product of
// all non-zeros divided by
//negative number with
// least absolute value
prod = prod / max_neg;
}
return prod;
}
// Driver Code
int main()
{
int a[] = { -1, -1, -2, 4, 3 };
int n = sizeof(a) / sizeof(a[0]);
printf("%d",maxProductSubset(a, n));
return 0;
}
// This code is contributed by rexomkar.
Java
// Java program to find maximum product of
// a subset.
class GFG {
static int maxProductSubset(int a[], int n) {
if (n == 1) {
return a[0];
}
// Find count of negative numbers, count
// of zeros, negative number
// with least absolute value
// and product of non-zero numbers
int max_neg = Integer.MIN_VALUE;
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 negative number
// with least absolute value.
if (a[i] < 0) {
count_neg++;
max_neg = Math.max(max_neg, a[i]);
}
prod = prod * a[i];
}
// If there are all zeros
if (count_zero == n) {
return 0;
}
// If there are odd number of
// negative numbers
if (count_neg % 2 == 1) {
// Exceptional case: There is only
// negative and all other are zeros
if (count_neg == 1
&& count_zero > 0
&& count_zero + count_neg == n) {
return 0;
}
// Otherwise result is product of
// all non-zeros divided by
//negative number with
// least absolute value.
prod = prod / max_neg;
}
return prod;
}
// Driver Code
public static void main(String[] args) {
int a[] = {-1, -1, -2, 4, 3};
int n = a.length;
System.out.println(maxProductSubset(a, n));
}
}
/* This JAVA code is contributed by Rajput-Ji*/
Python3
# Python3 program to find maximum product
# of a subset.
def maxProductSubset(a, n):
if n == 1:
return a[0]
# Find count of negative numbers, count
# of zeros, negative number
# with least absolute value
# and product of non-zero numbers
max_neg = -999999999999
count_neg = 0
count_zero = 0
prod = 1
for i in range(n):
# If number is 0, we don't
# multiply it with product.
if a[i] == 0:
count_zero += 1
continue
# Count negatives and keep
# track of negative number
# with least absolute value.
if a[i] < 0:
count_neg += 1
max_neg = max(max_neg, a[i])
prod = prod * a[i]
# If there are all zeros
if count_zero == n:
return 0
# If there are odd number of
# negative numbers
if count_neg & 1:
# Exceptional case: There is only
# negative and all other are zeros
if (count_neg == 1 and count_zero > 0 and
count_zero + count_neg == n):
return 0
# Otherwise result is product of
# all non-zeros divided
# by negative number
# with least absolute value
prod = int(prod / max_neg)
return prod
# Driver Code
if __name__ == '__main__':
a = [ -1, -1, -2, 4, 3 ]
n = len(a)
print(maxProductSubset(a, n))
# This code is contributed by PranchalK
C#
// C# Java program to find maximum
// product of a subset.
using System;
class GFG
{
static int maxProductSubset(int []a,
int n)
{
if (n == 1)
{
return a[0];
}
// Find count of negative numbers,
// count of zeros, negative number with
// least absolute value and product of
// non-zero numbers
int max_neg = int.MinValue;
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 negative number with
// least absolute value.
if (a[i] < 0)
{
count_neg++;
max_neg = Math.Max(max_neg, a[i]);
}
prod = prod * a[i];
}
// If there are all zeros
if (count_zero == n)
{
return 0;
}
// If there are odd number of
// negative numbers
if (count_neg % 2 == 1)
{
// Exceptional case: There is only
// negative and all other are zeros
if (count_neg == 1 && count_zero > 0 &&
count_zero + count_neg == n)
{
return 0;
}
// Otherwise result is product of
// all non-zeros divided by negative
// number with least absolute value.
prod = prod / max_neg;
}
return prod;
}
// Driver code
public static void Main()
{
int []a = {-1, -1, -2, 4, 3};
int n = a.Length;
Console.Write(maxProductSubset(a, n));
}
}
// This code is contributed by Rajput-Ji
PHP
<?php
// PHP program to find maximum
// product of a subset.
function maxProductSubset($a, $n)
{
if ($n == 1)
return $a[0];
// Find count of negative numbers,
// count of zeros, negative number
// with least absolute value and product of
// non-zero numbers
$max_neg = PHP_INT_MIN;
$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 negative number
// with least absolute value.
if ($a[$i] < 0)
{
$count_neg++;
$max_neg = max($max_neg, $a[$i]);
}
$prod = $prod * $a[$i];
}
// If there are all zeros
if ($count_zero == $n)
return 0;
// If there are odd number of
// negative numbers
if ($count_neg & 1)
{
// Exceptional case: There is only
// negative and all other are zeros
if ($count_neg == 1 &&
$count_zero > 0 &&
$count_zero + $count_neg == $n)
return 0;
// Otherwise result is product of
// all non-zeros divided by negative
// number with least absolute value.
$prod = $prod / $max_neg;
}
return $prod;
}
// Driver Code
$a = array(-1, -1, -2, 4, 3 );
$n = sizeof($a);
echo maxProductSubset($a, $n);
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// JavaScript program to find maximum
// product of a subset.
function maxProductSubset(a, n)
{
if (n == 1)
return a[0];
// Find count of negative numbers,
// count of zeros, negative number
// with least absolute value and product of
// non-zero numbers
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 number is 0, we don't
// multiply it with product.
if (a[i] == 0)
{
count_zero++;
continue;
}
// Count negatives and keep
// track of negative number
// with least absolute value.
if (a[i] < 0)
{
count_neg++;
max_neg = Math.max(max_neg, a[i]);
}
prod = prod * a[i];
}
// If there are all zeros
if (count_zero == n)
return 0;
// If there are odd number of
// negative numbers
if (count_neg & 1)
{
// Exceptional case: There is only
// negative and all other are zeros
if (count_neg == 1 &&
count_zero > 0 &&
count_zero + count_neg == n)
return 0;
// Otherwise result is product of
// all non-zeros divided by negative
// number with least absolute value.
prod = prod / max_neg;
}
return prod;
}
// Driver Code
let a = [-1, -1, -2, 4, 3 ];
let n = a.length;
document.write(maxProductSubset(a, n));
// This code is contributed
// by _saurabh_jaiswal
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Product of the maximums of all subsets of an array Given an array arr[] consisting of N positive integers, the task is to find the product of the maximum of all possible subsets of the given array. Since the product can be very large, print it to modulo (109 + 7). Examples: Input: arr[] = {1, 2, 3}Output:Explanation:All possible subsets of the given
13 min read
Maximum and Minimum Product Subsets Given a set, we need to find maximum and minimum possible product among all subsets of the set. Examples: Input : arr[] = {4, -2, 5}; Output: Maximum product = 20 Minimum product = -40 Maximum product is obtained by multiplying 4 5 Minimum product is obtained by multiplying 4, -2, 5 Input : arr[] =
7 min read
Maximum possible difference of two subsets of an array Given an array of n-integers. The array may contain repetitive elements but the highest frequency of any element must not exceed two. You have to make two subsets such that the difference of the sum of their elements is maximum and both of them jointly contain all elements of the given array along w
15+ min read
Sum of maximum elements of all subsets Given an array of integer numbers, we need to find sum of maximum number of all possible subsets. Examples: Input : arr = {3, 2, 5}Output : 28Explanation : Subsets and their maximum are,{} maximum = 0{3} maximum = 3{2} maximum = 2{5} maximum = 5{3, 2} maximum = 3{3, 5} maximum = 5{2, 5} maximum = 5{
9 min read
Find the maximum subset XOR of a given set Given a set of positive integers. find the maximum XOR subset value in the given set. Expected time complexity O(n).Examples:Input: set[] = {2, 4, 5}Output: 7The subset {2, 5} has maximum XOR valueInput: set[] = {9, 8, 5}Output: 13The subset {8, 5} has maximum XOR valueInput: set[] = {8, 1, 2, 12, 7
15+ min read
Split Array into maximum Subsets with same bitwise AND Given an array arr[] of size N, the task is to find the maximum number of subsets the array can be split such that the bitwise AND of the subsets is the same. Examples: Input: N = 4, arr[] = {1, 5, 2, 8}Output: 2Explanation:1st subset -> {1, 8}; bitwise AND = 0 2nd subset -> {2, 5}; bitwise AN
10 min read
Maximum XOR of k Size Subset Given an integer array arr[] of size n and an integer k. The task is to find the maximum XOR of the subset of size k from the given array arr[].Examples:Input: arr[] = [2, 5, 4, 1, 3, 7, 6, 8], k = 3Output: 15 Explanation: XOR of the elements of subset {2, 5, 8} is 15, which is the maximum possible.
14 min read
Maximum Sum of Array with given MEX Given 3 integers N, K, and X, the task is to construct an array arr[] with the below conditions: Size of the array = NMEX of the array = KAll array elements should be at most XAmong all the array that follows the above condition print the one having the maximum sum of its elements or print -1 if no
7 min read
Count of subsets having maximum possible XOR value Given an array arr[] consisting of N positive integers. The task is to count the number of different non-empty subsets of arr[] having maximum bitwise XOR. Examples: Input: arr[] = {3, 1}Output: 1Explanation: The maximum possible bitwise XOR of a subset is 3. In arr[] there is only one subset with b
6 min read
Maximum consecutive numbers present in an array Find the length of maximum number of consecutive numbers jumbled up in an array.Examples: Input : arr[] = {1, 94, 93, 1000, 5, 92, 78};Output : 3 The largest set of consecutive elements is92, 93, 94 Input : arr[] = {1, 5, 92, 4, 78, 6, 7};Output : 4 The largest set of consecutive elements is4, 5, 6,
15+ min read