Open In App

Find the Maximum Product of Two Integers in the Array using JavaScript ?

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of integers, we need to find the maximum product of two integers in the array using JavaScript.

Example:

Input: arr[] = {4, 6, 8, -5, -4, 4}  
Output: 48, {6,8}
Explanation: Maximum product is 48, which is obtained by
multiplying the numbers 6 and 8 of the array.

Brute Force Approach

The simplest approach to finding out which pair in an array of integers has the maximum product is to calculate the products of each pair by iterating through every possible pairing.

Example: This JavaScript code finds the pair of integers with the maximum product in an array. It iterates through pairs, calculates products, and returns the max pair with its product.

JavaScript
function findMaxProductPair(arr) {
    const n = arr.length;
    if (n < 2) {
        return null;
    }
    let maxProduct = -Infinity;
    let maxPair = [];

    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            const product = arr[i] * arr[j];
            if (product > maxProduct) {
                maxProduct = product;
                maxPair = [arr[i], arr[j]];
            }
        }
    }
    return maxPair;
}

const arr = [4, 6, 8, -5, -4, 4];
const maxPair = findMaxProductPair(arr);
if (maxPair !== null) {
    console.log("Maximum product pair: "
        + maxPair[0] + " and " + maxPair[1]);
    console.log("Maximum product: "
        + (maxPair[0] * maxPair[1]));
} else {
    console.log("No pair found.");
}

Output
Maximum product pair: 6 and 8
Maximum product: 48

Time Complexity: O(n2)

Auxiliary Space: O(1)

Sorting Approach

An alternative approach is to sort the array using sort() method ( in a non-decreasing order. after sorting of the array, the final two elements (largest in magnitude) can be considered as the possible pair with the maximum product.

Example: This JavaScript code finds the pair of integers with the maximum product in an array by sorting it first, then comparing products of the first two and last two elements to determine the maximum pair.

JavaScript
function findMaxProductPair(arr) {
    const n = arr.length;
    if (n < 2) {
        return null;
    }

    arr.sort((a, b) => a - b);

    const product1 = arr[0] * arr[1];
    const product2 = arr[n - 1] * arr[n - 2];
    return product1 > product2 ?
        [arr[0], arr[1]] : [arr[n - 1], arr[n - 2]];
}

const arr = [1, 2, -6, 3, -7];
const maxPair = findMaxProductPair(arr);

if (maxPair !== null) {
    console.log("Maximum product pair: "
        + maxPair[0] + " and " + maxPair[1]);
    console.log("Maximum product: "
        + (maxPair[0] * maxPair[1]));
} else {
    console.log("No pair found.");
}

Output
Maximum product pair: -7 and -6
Maximum product: 42

Time Complexity: O(nlog n)

Auxiliary Space: O(1)

Efficient Approach

An optimal approach must be taken into consideration when handling arrays containing negative integers. We can determine which pair in the array provides the maximum product by finding the maximum and minimum elements.

Example: This JavaScript function efficiently finds the pair of integers with the maximum product in an array by iteratively updating the maximum and minimum values, then comparing the products of the top two maximums and top two minimums to determine the maximum product pair.

JavaScript
function findMaxProductPair(array) {
    let max1 = -Infinity;
    let max2 = -Infinity;
    let min1 = Infinity;
    let min2 = Infinity;

    for (let num of array) {
        if (num > max1) {
            max2 = max1;
            max1 = num;
        } else if (num > max2) {
            max2 = num;
        }

        if (num < min1) {
            min2 = min1;
            min1 = num;
        } else if (num < min2) {
            min2 = num;
        }
    }


    const maxProductFromMax = max1 * max2;
    const maxProductFromMin = min1 * min2;
    const maxProduct = Math
        .max(maxProductFromMax, maxProductFromMin);
    const pair = maxProductFromMax >= maxProductFromMin ?
        [max1, max2] : [min1, min2];

    return { maxProduct, pair };
}

const array = [1, 2, -6, 3, -7];
const result = findMaxProductPair(array);

console.log("Maximum product pair: "
    + result.pair[0] + " and " + result.pair[1]);
console.log("Maximum product: "
    + result.maxProduct);

Output
Maximum product pair: -7 and -6
Maximum product: 42

Time complexity: O(n)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads