Java Program for Minimum product subset of an array Last Updated : 09 Dec, 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. 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 K kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read Like