Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding the Largest Triple Product Array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the only argument.
Based on the array taken in as input, the function should construct a new array of the same length based on the following criteria.
Any corresponding element of the output array should be the product of the three largest numbers encountered thus far. If the corresponding index is less than 3 (we have not encountered three elements yet) then the corresponding value should be -1. Although we can use non-unique values to calculate the product, those non-unique values should be present at different indices.
For example, if the input array is:
const arr = [1, 2, 3, 4, 5, 6];
Then the output should be:
const output = [-1, -1, 6, 24, 60, 120];
How It Works
The algorithm maintains the three largest elements seen so far and calculates their product at each step:
- For indices 0 and 1: Return -1 (not enough elements)
- For index 2: Product of first three elements
- For subsequent indices: Product of three largest elements encountered
Example Implementation
const arr = [1, 2, 3, 4, 5, 6];
const maximumTripleProduct = (arr = []) => {
const res = [];
const max = [arr[0], arr[1], arr[2]];
res[0] = res[1] = -1;
res[2] = arr[0] * arr[1] * arr[2];
for(let i = 3; i < arr.length; i++){
max.push(arr[i]);
max.sort((a, b) => b - a);
max.pop();
res[i] = max[0] * max[1] * max[2];
}
return res;
};
console.log(maximumTripleProduct(arr));
[-1, -1, 6, 24, 60, 120]
Step-by-Step Example
Let's trace through the algorithm with array [1, 2, 3, 4, 5, 6]:
const arr = [1, 2, 3, 4, 5, 6];
console.log("Index 0: Not enough elements ? -1");
console.log("Index 1: Not enough elements ? -1");
console.log("Index 2: Elements [1,2,3] ? Product:", 1 * 2 * 3);
console.log("Index 3: Largest 3 from [1,2,3,4] are [4,3,2] ? Product:", 4 * 3 * 2);
console.log("Index 4: Largest 3 from [1,2,3,4,5] are [5,4,3] ? Product:", 5 * 4 * 3);
console.log("Index 5: Largest 3 from [1,2,3,4,5,6] are [6,5,4] ? Product:", 6 * 5 * 4);
Index 0: Not enough elements ? -1 Index 1: Not enough elements ? -1 Index 2: Elements [1,2,3] ? Product: 6 Index 3: Largest 3 from [1,2,3,4] are [4,3,2] ? Product: 24 Index 4: Largest 3 from [1,2,3,4,5] are [5,4,3] ? Product: 60 Index 5: Largest 3 from [1,2,3,4,5,6] are [6,5,4] ? Product: 120
Alternative Approach Using Priority Queue Logic
Here's a more efficient approach that avoids sorting at each step:
const maximumTripleProductOptimized = (arr = []) => {
const res = [];
let first = -Infinity, second = -Infinity, third = -Infinity;
for(let i = 0; i < arr.length; i++){
if(i < 2){
res[i] = -1;
} else if(i === 2){
first = Math.max(arr[0], arr[1], arr[2]);
third = Math.min(arr[0], arr[1], arr[2]);
second = arr[0] + arr[1] + arr[2] - first - third;
res[i] = first * second * third;
} else {
if(arr[i] > first){
third = second;
second = first;
first = arr[i];
} else if(arr[i] > second){
third = second;
second = arr[i];
} else if(arr[i] > third){
third = arr[i];
}
res[i] = first * second * third;
}
}
return res;
};
const testArray = [1, 2, 3, 4, 5, 6];
console.log(maximumTripleProductOptimized(testArray));
[-1, -1, 6, 24, 60, 120]
Conclusion
The maximum triple product algorithm tracks the three largest elements encountered so far and calculates their product at each position. The optimized version avoids sorting by maintaining the three largest values directly, improving time complexity from O(n log n) to O(n).
