Write a Java program to find maximum product of two integers in a given array of integers.
Example:
Input :
nums = { 2, 3, 5, 7, -7, 5, 8, -5 }
Output:
Pair is (7, 8), Maximum Product: 56
public static void main(String[] args) {
List<Integer> sortedList = Stream.of( 2, 3, 5, 7, -7, 5, 8, -5)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
if (sortedList.size() >= 2) {
System.out.println("Pair is (" + sortedList.get(0) + ", " + sortedList.get(1) + "), Maximum Product: " + sortedList.get(0) * sortedList.get(1));
}
}
Comments
Leave a comment