Java Program to Find Maximum Product of Two Integers in an Array

In the previous article, we have seen Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller

In this article we are going to see how we can find maximum product of two integers in an array.

Java Program to Find Maximum Product of Two Integers in an Array

As we have to write a program which will find the maximum product of two integers in an array of integers so our first target should be finding first two biggest numbers from the array elements. As it is very much clear that product of two bigger number will give maximum result than product of two smaller numbers.

For example:

If we have an array say arr=[1, 7, 4, 2, 8, 6, 3, 9, 5]
Then in this array first two bigger numbers are 9 and 8.
So product is 9*8=72 which is maximum product we can find than any other possible combinations of array elements.

Let’s see different ways to find maximum product of two integers in an array.

Method-1: Java Program to Find Maximum Product of Two Integers in an Array By Using Sorting Technique

Approach:

  • Declare an array and with array elements.
  • Sort the array by using Arrays.sort() method.
  • Now array is sorted in ascending order.
  • So, find the product of last element and second last element which will give the maximum product value.
  • Print the result.

Program:

import java.util.Arrays;
import java.util.Comparator;
 
class Main
{
    public static void main(String[] args)
    {
        //integer array declared along with integer elements
        Integer[] input = { 1,6,3,4,5 };
        
        // sort the array in ascending order
        Arrays.sort(input);
        
        //as array is sorted in ascending order
        //so last two elements are biggest elements
        //so by multiplying the last two elemnts we will get maximum product
        long result=input[input.length-1]*input[input.length-2];
        //input[input.length-1] represents last elements
        //input[input.length-2] second last element
        System.out.println("Two numbers are "+ input[input.length-1] +" and "+ input[input.length-2]+" having maximum product "+result);        
    }
}
Output:

Two numbers are 6 and 5 having maximum product 30

Method-2: Java Program to Find Maximum Product of Two Integers in an Array By Finding First Two Biggest Element

Approach:

  • Take user input of length of array.
  • Take input of integer array elements.
  • Call the user defined method findBiggest() to find first 2 big elements in the array say firstNumbersecondNumber
  • After getting 2 big elements in the array, inside that findBiggest() method, call findMaxProduct() method.
  • Inside findMaxProduct() method multiply firstNumber and secondNumber which will give the maximum product value.
  • Print the result.

Program:

import java.util.Scanner;

public class Main 
{
     public static void main(String args[]) 
    {
        int length = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of elements in array: ");
        //array length taken as input
        length = sc.nextInt();
        //array declared
        int[] arr = new int[length];
        
        //take input of single digited number as array element
        System.out.println("Enter elements into array: ");
        //taking input of array elements
        for (int i = 0; i < length; i++) 
        {
            arr[i] = sc.nextInt();
        }

        System.out.println("First 2 biggest numbers in array : ");
        //calling findBiggest() method
        findBiggest(arr);
    }
    
    //user defined method to find first 2 biggest element
    private static void findBiggest(int[] arr) 
    {
        int firstNumber = arr[0];
        int secondNumber = arr[1];

        for (int i = 0; i < arr.length; i++) 
        { 
            if (arr[i] > firstNumber) 
            {
                secondNumber = firstNumber;
                firstNumber = arr[i];
            }
            else if (arr[i] > secondNumber) 
            {
                secondNumber = arr[i];
            } 
        }

        System.out.println("First biggest number : " + firstNumber);
        System.out.println("Second biggest number : " + secondNumber);
        
        //calling findMaxProduct() method to find maximum product
        findMaxProduct(firstNumber,secondNumber);
    }
    
    public static void findMaxProduct(int firstNumber, int secondNumber)
    {
        //multiplying both numbers to find product value
        int result= firstNumber*secondNumber;
        System.out.println("Two numbers are "+ firstNumber +" and "+ secondNumber+" having maximum product "+result);
    }
}
Output:

Enter number of elements in array: 
5
Enter elements into array: 
40 10 30 50 20
First 2 biggest numbers in array : 
First biggest number : 50
Second biggest number : 40
Two numbers are 50 and 40 having maximum product 2000

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: