All files / problems/highestProduct index.ts

100% Statements 29/29
100% Branches 2/2
100% Functions 1/1
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x  
// Given an array of N integers.
// Find the highest product by multiplying 3 elements.
// •Constraints:
// • 3 < N < 5e5
 
// Input: [1, 2, 3, 4]
// Output: 2 * 3 * 4 = 24
 
// Input: [0, -1, 10, 7, 5]
// Output: 5 * 7 * 10 = 350
const highestProduct = (arr: number[]): number => {
  const numbers: number[] = [];
  const sortedArr: number[] = arr.sort((a, b) => a - b);
 
  // 3 highest numbers
  const threeHighest =
    (sortedArr.at(-1) as number) *
    (sortedArr.at(-2) as number) *
    (sortedArr.at(-3) as number);
  // 2 lowest numbers and 1 highest number
  const twoLowestOneHighest =
    (sortedArr.at(0) as number) *
    (sortedArr.at(1) as number) *
    (sortedArr.at(-1) as number);
 
  return Math.max(threeHighest, twoLowestOneHighest);
};
 
export default highestProduct;