Answer to Question #309547 in Java | JSP | JSF for Kyle

Question #309547

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


1
Expert's answer
2022-03-16T10:25:03-0400
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));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS