C++ Program for Minimum product subset of an array Last Updated : 20 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an 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. 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; } Output: -24 Time Complexity : O(n) Auxiliary Space : O(1) Please refer complete article on Minimum product subset of an array for more details! Comment More infoAdvertise with us Next Article std::min in C++ K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Smallest index that splits an array into two subarrays with equal product Given an array(1-based indexing) arr[] consisting of N non zero integers, the task is to find the leftmost index i such that the product of all the elements of the subarrays arr[1, i] and arr[i + 1, N] is the same. Examples: Input: arr[] = {1, 2, 3, 3, 2, 1}Output: 3Explanation: Index 3 generates su 10 min read std::min in C++ The std::min() is used to find the minimum element among the given elements. It is the built-in function of C++ STL defined inside <algorithm> header file.Let's take the simplest example to demonstrate how the min() function works:C++#include <bits/stdc++.h> using namespace std; int main 3 min read std::min_element in C++ The std::min_element() in C++ is an STL algorithm that is used to find the minimum element in a given range. This range can be array, vector, list or any other container. It is defined inside the <algorithm> header file. In this article, we will learn about the std::min_element() in C++.Exampl 4 min read C++ Program for Minimum product pair an array of positive Integers Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array.Examples: Input : 11 8 5 7 5 100 Output : 25 Explanation : The minimum product of any two numbers will be 5 * 5 = 25. Input : 198 76 544 123 154 675 Output : 744 3 min read Print Minimum of all Subarrays using set in C++ STL Given an array of size N and an integer K, find the minimum for each and every contiguous subarray of size K. Examples: Input : arr[] = {5, 3, 4, 1, 1}, K = 3 Output : 3 1 1 Input : arr[] = {1, 2, 3, 4, 1, 6, 7, 8, 2, 1}, K = 4 Output : 1 1 1 1 1 2 1 Prerequisite: Sliding Window Technique Set in C++ 3 min read How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read Like