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);
}
}
Find two numbers of which the product is maximum in an array
Upasana | May 27, 2019 | 1 min read | 771 views | Java Coding Challenges
We can easily find two number in an array whose product is maximum using the below approach:
-
Sort the input integer array in descending order
-
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
Program output
maxNumber = 13 secondMaxNumber = 11 Product of min and max element = 143
That’s all.
Java Coding Challenges:
- Reverse position of words in a string using recursion
- Check if the given string is palindrome
- Prime number checker in Java
- Create anagram buckets from a given input array of words
- Anagrams string checker in Java
- Reverse a string using recursion in Java
- Java Program to find Factorial of a number
Top articles in this category:
- Java Program to find Factorial of a number
- Armstrong Number in Java
- Create anagram buckets from a given input array of words
- Java Coding Problems for SDET Automation Engineer
- What is Idempotent Operation? Which HTTP methods should be made idempotent
- Check whether given number is even or odd
- Prime number checker in Java
Recommended books for interview preparation:
Book you may be interested in..
Book you may be interested in..