Find two numbers of which the product is maximum in an array

Carvia Tech | May 27, 2019 | 1 min read | 219 views | Java Coding Challenges

We can easily find two number in an array whose product is maximum using the below approach:

  1. Sort the input integer array in descending order

  2. multiply first and second element of the array, the product will be maximum.

Java 8 Implementation

Find product of max two elements of an integer array
import java.util.Arrays;
import java.util.Comparator;

public class ArrayUtils {

    long productMinMax(Integer[] array) {
        Arrays.sort(array, Comparator.reverseOrder());
        int maxNumber = array[0];
        int secondMaxNumber = array[1];
        System.out.println("maxNumber = " + maxNumber);
        System.out.println("secondMaxNumber = " + secondMaxNumber);
        return secondMaxNumber * maxNumber;

    public static void main(String[] args) {
        long product = new ArrayUtils().productMinMax(new Integer[]{10, 11, 13, 9, 2, 4});
        System.out.println("Product of min and max element = " + product);
Program output
maxNumber = 13
secondMaxNumber = 11
Product of min and max element = 143

That’s all.

Java Coding Challenges:
  1. How to reverse a number in Java
  2. Check if the given number is Armstrong Number in Java
  3. Check a number is Prime: Java Coding Problem
  4. Calculate factorial of a number in Java using recursion
  5. Write a program to reverse a string using recursion in Java
  6. Java program to check if two strings are anagrams
  7. Create anagram buckets from a given input array of words
See all articles in Java Coding Challenges
Top articles in this category:
  1. Top 15 coding problems for SDET Java Interviews
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: Rest Assured Interview Questions
  4. SDET: JUnit interview questions for automation engineer
  5. Check if the given number is Armstrong Number in Java
  6. Check a number is Prime: Java Coding Problem
  7. How to check if the given number is palindrome in Java

Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info