Find the greatest product of three numbers in JavaScript


In the given problem statement our task is to find the greatest product of three numbers with the help of Javascript functionalities. So we will use sorting technique first to get the last three highest items of the array and then calculate the product of those three elements to get the desired result.

Understanding the Problem

The problem at hand is to find the greatest product of three items or numbers present in the given array in Javascript. So we will have an array of integers and we will identify three numbers whose product is the largest among all the possible combinations. For example suppose we have an array as [1, 5, 3, 2, 4] so in this array we have to calculate the product of three biggest numbers like (3 * 4 * 5) = 60. So the greatest product of three numbers is 60, which is the required result.

Logic for the given Problem

To solve the given problem for finding the greatest product of 3 numbers in the array, we need to check all the possible combinations of three integers from the given array. For doing this process we will initialize a maximum product as negative infinity. After that we will iterate through the array and then we will select three numbers at a time. And then we will calculate the product of them and compare the product with the maximum product calculated so far. Check the condition if the product is greater so we will update the value of maximum product.

Algorithm

Step 1: We have to find the greatest product of three numbers in the given array so for doing this task we will define a function and give it a name as findGreatestProduct. This function will accept a parameter of numbers array.

Step 2: So after defining the function we will initialize a variable to store the maximum product and give it name as maxProduct. Assign this variable a value as negative infinity.

Step 3: Now we will sort the given input array in ascending order which is an essential step to get the three biggest numbers from the array.

Step 4: After sorting the given array we will calculate the product of the first three numbers in the sorted array and assign it to the maxProduct variable.

Step 5: As we have calculated the product of first three numbers then we will iterate over the array and start from the index 2 because we have already computed the product of first three numbers.

Step 6: Now we will calculate the product of the current three numbers in the array. And check the condition if the current product is greater than maxProduct so we will update the maxProduct with the current product.

Step 7: After all the process we will return the maxProduct as the greatest product of three numbers.

Example

// Function to get the greatest product
function findGreatestProduct(nums) {
   const sortedNums = nums.sort((a, b) => a - b);
   const n = sortedNums.length;
   const product = (a, b, c) => a * b * c;

   let maxProduct = -Infinity;

   maxProduct = product(sortedNums[0], sortedNums[1], sortedNums[2]);

   for (let i = 2; i < n; i++) {
      const curProduct = product(
         sortedNums[i - 2],
         sortedNums[i - 1],
         sortedNums[i]
      );

      if (curProduct > maxProduct) {
         maxProduct = curProduct;
      }
   }

   return maxProduct;
}

// Usage of the function:
const nums = [1, 4, 3, 7, 6, 5, 9];
const greatestProduct = findGreatestProduct(nums);
console.log(greatestProduct);

Output

378

Complexity

The time complexity for finding the greatest product of the three numbers present in the array is O(n log n), in which n is the size of the input array. As we have used the sort method to sort the given array before traversing the items. And the time complexity of the sort method is O(log n). The space complexity of the code is O(1) because we have not used any additional data.

Conclusion

So with the created program we can effectively find the greatest product of the three numbers in the given array of integers. The code has given us a step by step process to solve the given problem. In the code we have considered all the possible combinations and update the maximum product as per that.

Updated on: 14-Aug-2023

316 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements