# Maximum product subset of an array in C++ program

In the problem, we are given an array arr[] of n integer values. Our task is to create a program to find the Maximum product subset of an array.

Problem Description − Here, we need to calculate the maximum possible product of a subset of elements of an array.

Subset − An array sub[] is a subset of array arr[] if all elements of sub[] are present in arr[].

Let’s take an example to understand the problem,

## Input

arr[] = {4, 5, 2, −1, 3}

## Output

40

## Explanation

Subset sub[] = {4, 5, 2}
Prod = 4*5*2 = 40

## Solution Approach

A simple and easy approach to solve the problem is by creating all possible subsets of the array. Find their products and return the maximum of them.

This solution is easy to implement but requires nested loops making its complexity of the order of O(n2*n).

Try implementation here.

Effective solution is by calculating the number of negative’s(nofNeg) and zero’s(nof0) from the array and then calculate the maxProd based on these conditions.

Case 1(nof0 = 0 and nofNeg is even)− Consider all elements of the array to the product.

maxProd = arr * arr * … arr[n−1]

Case 2(nof0 = 0 and nofNeg is odd)− Consider all elements of the array except the largest negative element of the array (nearest to 0).

Case 3(nof0 != 0)− Leave all zeros of the array for the product. And take cases similar to cases 1 and 2.

Special Case − only one element except 0 is negative. maxProd = 0.

## Algorithm

Initialise

maxProd = 1;

Step 1

Loop the array and count, nof0(number of zeros) and
nofNeg(number of negatives), and find maxProd.
maxProd = maxProd*arr[i], i −> 0 to n−1

Step 2

consider the following cases −
Case 1− if(nofNeg%2 == 0): maxProd
Case 2− if(nofNeg%2 != 0): maxProd = maxProd/(largestNeg)
Case 3 − if(nof0 == (n-1) and nofNeg == 1), maxProd = 0.

## Step 3

Print maxProd.

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
int findMaxSubsetProd(int arr[], int n){
int larNeg = −1000;
int nofNeg = 0, Nof0 = 0;
int maxProd = 1;
for (int i = 0; i < n; i++) {
if (arr[i] == 0){
Nof0++;
continue;
}
else if (arr[i] < 0) {
nofNeg++;
if(larNeg < arr[i])
larNeg = arr[i];
}
maxProd = maxProd * arr[i];
}
if(nofNeg%2 == 0){
return maxProd;
}
else if(nofNeg%2 != 0)
return (maxProd / larNeg);
if(Nof0 == (n−1) and nofNeg == 1)
return 0;
return maxProd;
}
int main(){
int arr[] = {4, −2, 5, −1, 3, −6};
int n = sizeof(arr)/sizeof(arr);
cout<<"The maximum product subset of an array is "<<findMaxSubsetProd(arr, n);
return 0;
}

## Output

The maximum product subset of an array is 720
{"ft_lang_label":"__label__en","ft_lang_prob":0.6277393,"math_prob":0.995195,"size":4330,"snap":"2023-14-2023-23","text_gpt3_token_len":1246,"char_repetition_ratio":0.18908922,"word_repetition_ratio":0.07566204,"special_character_ratio":0.30554274,"punctuation_ratio":0.082914576,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99883145,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-28T03:37:03Z\",\"WARC-Record-ID\":\"<urn:uuid:8680d31a-fbfc-435f-abff-dfb91fdc04a7>\",\"Content-Length\":\"40738\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:16afc6d8-4f78-40c5-b7c2-9f6a2c003f33>\",\"WARC-Concurrent-To\":\"<urn:uuid:72f5ef33-edbd-426d-b41c-6a45730039eb>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/maximum-product-subset-of-an-array-in-cplusplus-program\",\"WARC-Payload-Digest\":\"sha1:W6ITNEJCBVG4LF4SFTZOA26XPI3XIAB4\",\"WARC-Block-Digest\":\"sha1:KDGZMCP3SM3QINVW7HHDP2CZXSZLQVVK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296948756.99_warc_CC-MAIN-20230328011555-20230328041555-00394.warc.gz\"}"}