Java Program for Minimum product subset of an array Last Updated : 09 Dec, 2022 Comments Improve Suggest changes 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. Java // Java program to find maximum product of // a subset. import java.io.*; public class GFG { static 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 negmax = Integer.MIN_VALUE; int posmin = Integer.MAX_VALUE; int count_neg = 0, count_zero = 0; int product = 1; for (int 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 = product / negmax; } return product; } // main function public static void main(String[] args) { int a[] = { -1, -1, -2, 4, 3 }; int n = 5; System.out.println(minProductSubset(a, n)); } } // This code is contributed by Arnab Kundu. 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 Java Program for Minimum product subset of an array kartik Follow Improve Article Tags : Java Greedy Sorting Technical Scripter Java Programs DSA Arrays +3 More Practice Tags : ArraysGreedyJavaSorting Similar Reads 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 Maximum Product Subarray Given an array that contains both positive and negative integers, find the product of the maximum product subarray. Expected Time complexity is O(n) and only O(1) extra space can be used. Examples: Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, 5 min read Java Program to Minimize the Maximum Element of an Array Given two integers N and K. Create an array of N positive integers such that the sum of all elements of the array is divisible by K and the maximum element in the array is the minimum possible. You have to find the Maximum element of that array. Example: Input : N=5, K=11 Output : 3 Explanation : We 4 min read Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value 3 min read Java 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}; 4 min read Finding Minimum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic 3 min read Java Program for Find the subarray with least average Given an array arr[] of size n and integer k such that k Examples :Â Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3 Output: Subarray between indexes 3 and 5 The subarray {20, 10, 50} has the least average among all subarrays of size 3. Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2 Output: Subarr 3 min read Java Program to Solve Set Cover Problem Problem Statement: Given a set of elements 1 to n and a set S of n sets whose union equals everything, the problem is to find the minimum numbers of subsets equal the set in a pair of 2. Concept: This problem is to solve the set problems. We can use permutations and combinations to solve this proble 5 min read Java Program to Find the smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m = 5 min read Java Program to Find k pairs with smallest sums in two arrays Given two integer arrays arr1[] and arr2[] sorted in ascending order and an integer k. Find k pairs with smallest sums such that one element of a pair belongs to arr1[] and other element belongs to arr2[]Examples: Input : arr1[] = {1, 7, 11} arr2[] = {2, 4, 6} k = 3 Output : [1, 2], [1, 4], [1, 6] E 3 min read Like