|
| 1 | +/** |
| 2 | + * Calculates the highest product of three numbers. |
| 3 | + * @param {Number[]} arrayOfInts An array of integers with a minimum length of 3. |
| 4 | + * @return {Number} The highest product of three numbers. |
| 5 | + */ |
| 6 | +function highestProductOf3(arrayOfInts) { |
| 7 | + // Throw error if given array has fewer than 3 integers. |
| 8 | + if (arrayOfInts.length < 3) |
| 9 | + throw new Error("Minimum of three integers needed."); |
| 10 | + |
| 11 | + // Track the greatest 3 positive values in descending order |
| 12 | + const highestPositives = [-Infinity, -Infinity, -Infinity]; |
| 13 | + // Track the 2 lowest values in ascending order (for negative-to-positive product) |
| 14 | + const lowestNegatives = [Infinity, Infinity]; |
| 15 | + |
| 16 | + // Sweep through all given integers once. |
| 17 | + for (let i = 0; i < arrayOfInts.length; i++) { |
| 18 | + const currentInt = arrayOfInts[i]; |
| 19 | + |
| 20 | + // Check if current integer is one of the greatest 3 values seen thus far |
| 21 | + // Swap as needed (cascading swaps may occur). |
| 22 | + let temp = currentInt; |
| 23 | + let temp2 = temp; |
| 24 | + |
| 25 | + // Largest value |
| 26 | + if (temp > highestPositives[0]) { |
| 27 | + temp2 = highestPositives[0]; |
| 28 | + highestPositives[0] = temp; |
| 29 | + temp = temp2; |
| 30 | + } |
| 31 | + // 2nd Largest Value |
| 32 | + if (temp > highestPositives[1]) { |
| 33 | + temp2 = highestPositives[1]; |
| 34 | + highestPositives[1] = temp; |
| 35 | + temp = temp2; |
| 36 | + } |
| 37 | + // 3rd Largest Value |
| 38 | + if (temp > highestPositives[2]) { |
| 39 | + temp2 = highestPositives[2]; |
| 40 | + highestPositives[2] = temp; |
| 41 | + temp = temp2; |
| 42 | + } |
| 43 | + |
| 44 | + // Check if current integer is one of the least 2 negative values seen thus far |
| 45 | + // Swap as needed (cascading swaps may occur). |
| 46 | + if (currentInt < 0) { |
| 47 | + temp = currentInt; |
| 48 | + temp2 = temp; |
| 49 | + // Least Negative Value |
| 50 | + if (temp < lowestNegatives[0]) { |
| 51 | + temp2 = lowestNegatives[0]; |
| 52 | + lowestNegatives[0] = temp; |
| 53 | + temp = temp2; |
| 54 | + } |
| 55 | + // 2nd Least Negative Value |
| 56 | + if (temp < lowestNegatives[1]) { |
| 57 | + temp2 = lowestNegatives[1]; |
| 58 | + lowestNegatives[1] = temp; |
| 59 | + temp = temp2; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + // Calculate the product of the greatest 3 positive values, |
| 64 | + const highest3 = |
| 65 | + highestPositives[0] * highestPositives[1] * highestPositives[2]; |
| 66 | + |
| 67 | + // If at least two negative values were found |
| 68 | + if (lowestNegatives[0] !== Infinity && lowestNegatives[1] !== Infinity) { |
| 69 | + // Calculate the product of the greatest positive value with the 2 lowest negative values, |
| 70 | + // (Negative multiplied by a negative gives a positive) |
| 71 | + const highestWithLowest2 = |
| 72 | + highestPositives[0] * lowestNegatives[0] * lowestNegatives[1]; |
| 73 | + // Return the greater result. |
| 74 | + return Math.max(highest3, highestWithLowest2); |
| 75 | + } |
| 76 | + |
| 77 | + return highest3; |
| 78 | +} |
| 79 | + |
| 80 | +export default highestProductOf3; |
0 commit comments