Minimum Product Pair in an Array of Positive Integers

Last Updated : 13 Jun, 2026

Given an array arr[] of positive integers. The task is to print the minimum product of any two numbers of the given array.

Examples: 

Input: arr[] = [2, 7, 3, 4]
Output: 6
Explanation: The minimum product of any two numbers will be 2 * 3 = 6.

Input: arr[] = [198, 76, 544, 123, 154, 675]
Output:  9348
Explanation: The minimum product of any two numbers will be 76 * 123 = 9348.

Try It Yourself
redirect icon

[Naive Approach] Check All Pairs – O(n ^ 2) Time O(1) Space

The idea is to generate all possible pairs of elements in the array and compute their product. Keep track of the minimum product encountered during the traversal.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to find the minimum product pair in the array
int printMinimumProduct(vector<int> &arr)
{
    int n = arr.size();

    // Initialize answer with maximum possible value
    int mini = INT_MAX;

    // Generate all possible pairs
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            // Update minimum product if current pair gives a smaller product
            mini = min(mini, arr[i] * arr[j]);
        }
    }

    // Return the minimum product found
    return mini;
}

// Driver Code
int main()
{
    vector<int> arr = {198, 76, 544, 123, 154, 675};

    cout << printMinimumProduct(arr);

    return 0;
}
Java
import java.util.Arrays;

public class GfG {
    // Function to find the minimum product pair in the
    // array
    public static int printMinimumProduct(int[] arr)
    {
        int n = arr.length;

        // Initialize answer with maximum possible value
        int mini = Integer.MAX_VALUE;

        // Generate all possible pairs
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                // Update minimum product if current pair
                // gives a smaller product
                mini = Math.min(mini, arr[i] * arr[j]);
            }
        }

        // Return the minimum product found
        return mini;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 198, 76, 544, 123, 154, 675 };
        System.out.println(printMinimumProduct(arr));
    }
}
Python
def printMinimumProduct(arr):
    n = len(arr)

    # Initialize answer with maximum possible value
    mini = float('inf')

    # Generate all possible pairs
    for i in range(n):
        for j in range(i + 1, n):
            # Update minimum product if current pair gives a smaller product
            mini = min(mini, arr[i] * arr[j])

    # Return the minimum product found
    return mini


# Driver Code
if __name__ == "__main__":
    arr = [198, 76, 544, 123, 154, 675]

    print(printMinimumProduct(arr))
C#
using System;

class GfG {
    // Function to find the minimum product pair in the
    // array
    public static int PrintMinimumProduct(int[] arr)
    {
        int n = arr.Length;

        // Initialize answer with maximum possible value
        int mini = int.MaxValue;

        // Generate all possible pairs
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                // Update minimum product if current pair
                // gives a smaller product
                mini = Math.Min(mini, arr[i] * arr[j]);
            }
        }

        // Return the minimum product found
        return mini;
    }

    // Driver Code
    public static void Main()
    {
        int[] arr = { 198, 76, 544, 123, 154, 675 };
        Console.WriteLine(PrintMinimumProduct(arr));
    }
}
JavaScript
function printMinimumProduct(arr) {
    let n = arr.length;

    // Initialize answer with maximum possible value
    let mini = Number.MAX_SAFE_INTEGER;

    // Generate all possible pairs
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            // Update minimum product if current pair gives a smaller product
            mini = Math.min(mini, arr[i] * arr[j]);
        }
    }

    // Return the minimum product found
    return mini;
}

// Driver Code
let arr = [198, 76, 544, 123, 154, 675];
console.log(printMinimumProduct(arr));

Output
9348

Time Complexity: O(n ^ 2)
Auxiliary Space: O(1)

[Expected Approach] Find Two Smallest Elements – O(n) Time O(1) Space

The idea is to find the smallest and second smallest elements in a single traversal of the array. Since all elements are positive, the minimum product will always be formed by the two smallest numbers.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to find the minimum product pair in the array
int printMinimumProduct(vector<int> &arr)
{

    // Initialize mn1 and mn2 to the maximum possible integer value.
    // mn1 will store the smallest number, and mn2 will store the second smallest
    // number.
    int mn1 = INT_MAX;
    int mn2 = INT_MAX;

    int n = arr.size();

    // Traverse the array to find the two smallest elements
    for (int i = 0; i < n; i++)
    {

        // If current element is smaller than mn1
        if (arr[i] < mn1)
        {

            // Update mn2 to the previous smallest element
            mn2 = mn1;

            // Update mn1 to the current element
            mn1 = arr[i];
        }

        // If current element is not smaller than mn1 but smaller than mn2
        else if (arr[i] < mn2)
        {

            // Update mn2 to the current element
            mn2 = arr[i];
        }
    }

    // Return the product of the two smallest numbers in the array
    return mn1 * mn2;
}

// Driver Code
int main()
{
    vector<int> arr = {198, 76, 544, 123, 154, 675};

    cout << printMinimumProduct(arr);

    return 0;
}
Java
import java.util.Arrays;

public class GfG {
    // Function to find the minimum product pair in the
    // array
    public static int printMinimumProduct(int[] arr)
    {

        // Initialize mn1 and mn2 to the maximum possible
        // integer value. mn1 will store the smallest
        // number, and mn2 will store the second smallest
        // number.
        int mn1 = Integer.MAX_VALUE;
        int mn2 = Integer.MAX_VALUE;

        int n = arr.length;

        // Traverse the array to find the two smallest
        // elements
        for (int i = 0; i < n; i++) {

            // If current element is smaller than mn1
            if (arr[i] < mn1) {

                // Update mn2 to the previous smallest
                // element
                mn2 = mn1;

                // Update mn1 to the current element
                mn1 = arr[i];
            }

            // If current element is not smaller than mn1
            // but smaller than mn2
            else if (arr[i] < mn2) {

                // Update mn2 to the current element
                mn2 = arr[i];
            }
        }

        // Return the product of the two smallest numbers in
        // the array
        return mn1 * mn2;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 198, 76, 544, 123, 154, 675 };

        System.out.println(printMinimumProduct(arr));
    }
}
Python
def printMinimumProduct(arr):

    # Initialize mn1 and mn2 to the maximum possible integer value.
    # mn1 will store the smallest number, and mn2 will store the second smallest
    # number.
    mn1 = float('inf')
    mn2 = float('inf')

    n = len(arr)

    # Traverse the array to find the two smallest elements
    for i in range(n):

        # If current element is smaller than mn1
        if arr[i] < mn1:

            # Update mn2 to the previous smallest element
            mn2 = mn1

            # Update mn1 to the current element
            mn1 = arr[i]

        # If current element is not smaller than mn1 but smaller than mn2
        elif arr[i] < mn2:

            # Update mn2 to the current element
            mn2 = arr[i]

    # Return the product of the two smallest numbers in the array
    return mn1 * mn2


# Driver Code
if __name__ == "__main__":
    arr = [198, 76, 544, 123, 154, 675]

    print(printMinimumProduct(arr))
C#
using System;

public class GfG {
    // Function to find the minimum product pair in the
    // array
    public static int printMinimumProduct(int[] arr)
    {

        // Initialize mn1 and mn2 to the maximum possible
        // integer value. mn1 will store the smallest
        // number, and mn2 will store the second smallest
        // number.
        int mn1 = int.MaxValue;
        int mn2 = int.MaxValue;

        int n = arr.Length;

        // Traverse the array to find the two smallest
        // elements
        for (int i = 0; i < n; i++) {

            // If current element is smaller than mn1
            if (arr[i] < mn1) {

                // Update mn2 to the previous smallest
                // element
                mn2 = mn1;

                // Update mn1 to the current element
                mn1 = arr[i];
            }

            // If current element is not smaller than mn1
            // but smaller than mn2
            else if (arr[i] < mn2) {

                // Update mn2 to the current element
                mn2 = arr[i];
            }
        }

        // Return the product of the two smallest numbers in
        // the array
        return mn1 * mn2;
    }

    // Driver Code
    public static void Main()
    {
        int[] arr = { 198, 76, 544, 123, 154, 675 };

        Console.WriteLine(printMinimumProduct(arr));
    }
}
JavaScript
function printMinimumProduct(arr) {

    // Initialize mn1 and mn2 to the maximum possible integer value.
    // mn1 will store the smallest number, and mn2 will store the second smallest
    // number.
    let mn1 = Number.MAX_VALUE;
    let mn2 = Number.MAX_VALUE;

    let n = arr.length;

    // Traverse the array to find the two smallest elements
    for (let i = 0; i < n; i++) {

        // If current element is smaller than mn1
        if (arr[i] < mn1) {

            // Update mn2 to the previous smallest element
            mn2 = mn1;

            // Update mn1 to the current element
            mn1 = arr[i];
        }

        // If current element is not smaller than mn1 but smaller than mn2
        else if (arr[i] < mn2) {

            // Update mn2 to the current element
            mn2 = arr[i];
        }
    }

    // Return the product of the two smallest numbers in the array
    return mn1 * mn2;
}

// Driver Code
let arr = [198, 76, 544, 123, 154, 675];

console.log(printMinimumProduct(arr));

Output
9348

Time Complexity: O(n)
Auxiliary Space: O(1)

Comment