PHP Program for Minimum Product Subset of an Array Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given an array Arr, 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 : Arr = [ -1, -1, -2, 4, 3 ] Output : -24 Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24 Input : Arr = [ -1, 0 ] Output : -1 Explanation : -1(single element) is minimum product possible Input : Arr = [ 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. PHP <?php // PHP program to find maximum // product of a subset. // Function 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 $max_neg = PHP_INT_MIN; $min_pos = PHP_INT_MAX; $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 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; } // Driver code $a = [-1, -1, -2, 4, 3]; $n = sizeof($a); echo minProductSubset($a, $n); ?> Output-24Time 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 PHP Program for Minimum Product Subset of an Array kartik Follow Improve Article Tags : Greedy Sorting Technical Scripter Web Technologies PHP PHP Programs DSA Arrays +4 More Practice Tags : ArraysGreedySorting Similar Reads PHP Program for Maximum Equilibrium Sum in an Array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[].Examples: Input : arr[] = {-1, 2, 3, 0, 3, 2, -1}Output : 4Prefix sum of arr[0..3] = Suffix sum of arr[3..6]Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2}Output : 7Prefix sum of arr[0..3] = Suffix su 3 min read PHP Program for Maximum and Minimum in a Square Matrix Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : Arr = [ [ 5, 4, 9 ], [ 2, 0, 6 ], [ 3, 1, 8 ] ]; Output : Maximum = 9, Minimum = 0 Input : Arr = [[ -5, 3 ], [ 2, 4 ]]; Output : Maximum = 4, Minimum = -5Naive MethodWe find the maximum and mini 2 min read PHP Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples:Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input : arr[] = {2, 5, 6, 7, 8, 8, 9}; 3 min read PHP Program for Maximum Difference Between Groups of Size Two Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : Ar 3 min read PHP Program for Number of pairs with maximum sum Write a PHP program for a given array arr[], count the number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j.Example:Input : arr[] = {1, 1, 1, 2, 2, 2} Output: 3 Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the 3 min read PHP Program to Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[] 4 min read PHP Program to Find the Size of Subarray With Maximum Sum Given an Array, the task is to find the size of the subarray that yields the maximum sum. This article provides a comprehensive guide on how to implement a PHP program to solve this problem efficiently.Examples: Input: Arr = [ 1, -2, 1, 1, -2, 1 ]Output: Length of the subarray is 2Explanation: The s 2 min read PHP Program for Find the Subarray with Least Average Given an array Arr of size n and integer k such that k <= n, the task is to find the subarray with least average.Examples:Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3Output: Subarray between indexes 3 and 5The subarray {20, 10, 50} has the least average among all subarrays of size 3.Input: ar 3 min read Check Whether an Array is Subset of Another Array in PHP Given two arrays i.e. subset and superset, the task is to check whether an array is subset of another array in PHP. This article covers various methods to determine whether an array is a subset of another array.These are the following approaches:Table of ContentUsing array_diff() FunctionUsing array 4 min read PHP Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: The third Largest element is 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Inp 5 min read Like