Javascript Program for Minimum product subset of an array Last Updated : 12 Sep, 2024 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 : -24Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24Input : a[] = { -1, 0 }Output : -1Explanation : -1(single element) is minimum product possible Input : a[] = { 0, 0, 0 }Output : 0A 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. JavaScript // Javascript program to find maximum // product of a subset. function minProductSubset(a, 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 let negmax = Number.MIN_VALUE; let posmin = Number.MIN_VALUE; let count_neg = 0, count_zero = 0; let product = 1; for (let i = 0; i < n; i++) { // If number is zero, count it // but dont multiply if (a[i] == 0) { count_zero++; continue; } // Count the negative numbers // and find the max negative number if (a[i] < 0) { count_neg++; negmax = Math.max(negmax, a[i]); } // Find the minimum positive number if (a[i] > 0 && a[i] < posmin) { posmin = a[i]; } product *= a[i]; } // If there are all zeroes // or zero is present but no // negative number is present if (count_zero == n || (count_neg == 0 && count_zero > 0)) return 0; // If there are all positive if (count_neg == 0) return posmin; // If there are even number except // zero of negative numbers if (count_neg % 2 == 0 && count_neg != 0) { // Otherwise result is product of // all non-zeros divided by maximum // valued negative. product = parseInt(product / negmax, 10); } return product; } // Driver code let a = [-1, -1, -2, 4, 3]; let n = 5; console.log(minProductSubset(a, n)); // This code is contributed by rameshtravel07 Output-24 Complexity Analysis: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 Javascript Program for Minimum product subset of an array K kartik Follow Improve Article Tags : JavaScript Similar Reads Javascript Program For Chocolate Distribution Problem Given an array of n integers where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are m students, the task is to distribute chocolate packets such that: Each student gets one packet.The difference between the number of chocolat 3 min read Java 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 : 934 4 min read Java Program for Minimum product subset of an array 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[ 3 min read 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 100Output: 25 Explanation: The minimum product of any two numbers will be 5 * 5 = 25. Input: 198 76 544 123 154 675 Output: 7448Expl 12 min read C++ Program for Minimum product subset of an array 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 3 min read Like