- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the greatest product of three numbers in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the only. The function should prepare an array of three such elements from the array that yields the greatest products amongst any three elements of the array. The function should finally return the product of those three elements.
For example −
If the input array is −
const arr = [-10, 7, 29, 30, 5, -10, -70];
Then the output should be −
const output = 21000
because the three numbers are 30, -10, -70
We can see that the array can or will probably contain negative elements. Therefore, in such a case the maximum product will be the greater of these two −
min1 * min2 * max1 max1 * max2 * max3
Therefore, we will solve it just like this
Example
Following is the code −
const arr = [-10, 7, 29, 30, 5, -10, -70]; const threeProduct = (arr = []) => { const sorter = (a, b) => a -b; arr.sort(sorter); let pro1 = 1, pro2 = 1; let len = arr.length - 1; for (let i = len; i > len - 3; i--) { pro1 = pro1 * arr[i]; }; pro2 = arr[0] * arr[1] * arr[len]; return Math.max(pro1, pro2); } console.log(threeProduct(arr));
Output
Following is the output on console −
21000
- Related Articles
- Return the greatest possible product of n numbers from the array in JavaScript
- Subarray with the greatest product in JavaScript
- Maximum Product of Three Numbers in C++
- The sum of three numbers in A.P. is $3$ and their product is $- 35$. Find the numbers.
- Three numbers are in A.P. If the sum of these numbers be 27 and the product 648, find the numbers.
- Program to find largest of three numbers - JavaScript
- Find the product of the greatest 5 digit number and greatest 4 digital number by using the distributive property.
- Find the greatest number of three digits which is a perfect square.
- The sum of the three numbers in an arithmetic progression is 18. If the product of the first and third number is 5 times the common difference, find the three numbers.
- How to find the greatest number in a list of numbers in Python?
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- Product of all other numbers an array in JavaScript
- Find the Product of first N Prime Numbers in C++
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Product of numbers present in a nested array in JavaScript

Advertisements