JavaScript program for Minimum Product Subset of an Array


JavaScript Program for Minimum product subset of an array is a common problem that arises in the field of computer science and programming. The problem statement requires us to find the minimum product that can be obtained from any subset of the given array.

A minimum product subset of an array is a subset of array elements that yields the smallest possible product. Several algorithms are available for identifying this subset, including dynamic programming, greedy algorithms, and branch and bound. The selection of the algorithm is determined by the particular constraints and specifications of the problem at hand.

In this tutorial, we will discuss the various approaches to solving this problem using the JavaScript programming language. We will cover the basic algorithmic approach and its implementation using JavaScript code snippets. By the end of this tutorial, readers will have a clear understanding of the problem statement and various approaches to solving it using JavaScript.

Problem Statement

Given an array of integers, we need to find the minimum product subset of the array. A product subset of an array is defined as the product of any subset of the array.

For example,

Let’s consider the array [2, 3, -1, 4, -2].

The product subsets of this array are

[2], [3], [-1], [4], [-2], [2, 3], [2, -1], [2, 4], [2, -2], [3, -1], [3, 4], [3, -2], [-1, 4], [-1, -2], [4, -2], [2, 3, -1], [2, 3, 4], [2, 3, -2], [2, -1, 4], [2, -1, -2], [2, 4, -2], [3, -1, 4], [3, -1, -2], [3, 4, -2], [-1, 4, -2], and [2, 3, -1, 4, -2]. 

The minimum product subset of this array is [-2].

Now let’s discuss the various algorithmic approaches to solve this problem statement and choose the best-suited algorithm.

Algorithm

The selection of the algorithm relies on the specific limitations and prerequisites of the problem.

The Greedy Algorithm − The greedy algorithm is a frequently used method to discover the minimum product subset of an array. Its fundamental concept is to commence with the initial array element and append the next element to the subset solely if it will generate a smaller product. Despite its ease of implementation and simplicity, the greedy algorithm may not necessarily provide the optimal solution, and its performance can be notably sluggish for vast arrays.

Dynamic Programming − Dynamic programming is another algorithm utilized to tackle this issue. It separates the problem into smaller subproblems and resolves each subproblems once, exploiting the solutions of tinier subproblems to determine the solution for larger ones. This methodology can result in substantial time and space conservation. While dynamic programming guarantees an optimal solution, it can be more intricate to implement than the greedy algorithm.

Branch and Bound algorithm − Another approach for identifying the minimum product subset of an array is the branch and bound algorithm. It entails exploring multiple possibilities through branching and constraining the search to only consider valid solutions. This algorithm guarantees an optimal solution and can be quicker than other algorithms for specific scenarios. Nonetheless, its implementation can be more intricate, and it may necessitate more time and space resources than other algorithms.

In conclusion, an unsophisticated approach entails generating all subsets, computing the product of each subset, and returning the minimum product.

A superior solution involves considering the following facts.

  • STEP 1 − In the absence of zeros and with an even number of negative numbers, the product of all elements except the most substantial negative number will yield the result.

  • STEP 2 − In the absence of zeros and with an odd number of negative numbers, the product of all elements will provide the result.

  • STEP 3 − In the presence of zeros and exclusively positive numbers, the outcome is 0. However, in the exceptional case where no negative numbers exist, and all other elements are positive, the answer should be the smallest positive number.

Now let’s try to understand the above-mentioned approaches with an example where we implement the problem statement using JavaScript.

Example

This program it first calculates the count of negative numbers, zeros, maximum valued negative numbers, minimum valued positive numbers and products of non-zero numbers. It then applies the rules based on the count of negative numbers and zeros to return the minimum product subset of the array. The time complexity of the program is O(n) and the auxiliary space is O(1).

Input 1: a[] = { -1, -1, -2, 4, 3 }; n = 5

Expected Output: The minimum subset is [ -2, 4, 3 ] and the minimum product is -24.

Input 2: a[] = { -1, 0 }; n = 2

Expected Output: The minimum subset is [ -1 ] and the minimum product is -1.

function minProductSubset(a, n) {
   if (n === 1) {
      return [a[0], a[0]];
   }
   let negmax = Number.NEGATIVE_INFINITY;
   let posmin = Number.POSITIVE_INFINITY;
   let count_neg = 0, count_zero = 0;
   let subsets = [[]];  
   for (let i = 0; i < n; i++) {
      if (a[i] === 0) {
         count_zero++;
         continue;
      }
      if (a[i] < 0) {
         count_neg++;
         negmax = Math.max(negmax, a[i]);
      }
      if (a[i] > 0 && a[i] < posmin) {
         posmin = a[i];
      }
      const subsetsLength = subsets.length;
      for(let j = 0; j < subsetsLength; j++){
         const subset = [...subsets[j], a[i]];
         subsets.push(subset);
      }
   }
   if (count_zero === n || (count_neg === 0 && count_zero > 0)) {
      return [0, 0];
   }
   if (count_neg === 0) {
      return [posmin, posmin];
   }
   const negativeSubsets = subsets.filter(subset => subset.reduce((acc, cur) => acc * cur, 1) < 0);
   let minSubset = negativeSubsets[0];
   let minProduct = minSubset.reduce((acc, cur) => acc * cur, 1);
   for (let i = 1; i < negativeSubsets.length; i++) {
      const product = negativeSubsets[i].reduce((acc, cur) => acc * cur, 1);
      if (product < minProduct) {
         minSubset = negativeSubsets[i];
         minProduct = product;
      }
   }  
   return [minSubset, minProduct];
}
let a = [-1, -1, -2, 4, 3];
let n = 5;
const [minSubset, minProduct] = minProductSubset(a, n);
console.log(`The minimum subset is [ ${minSubset.join(', ')} ] and the minimum product is ${minProduct}.`);

Conclusion

So in this tutorial, we learnt about finding the minimum product subset of an array using JavaScript by following a simple algorithm. The solution involves various criteria such as the number of negative numbers, positive numbers, and zeros present in the array. It uses simple if-else conditions to check for these criteria and returns the minimum product subset accordingly. The time complexity of the program is O(n), and the auxiliary space required is O(1).

Updated on: 02-May-2023

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements