Skip to content

Instantly share code, notes, and snippets.

@vignarajj
Last active February 13, 2019 04:28
Show Gist options
  • Save vignarajj/86c867fbce61d39cf92231e96b0563f2 to your computer and use it in GitHub Desktop.
Save vignarajj/86c867fbce61d39cf92231e96b0563f2 to your computer and use it in GitHub Desktop.
Find the maximum products of any 2 numbers in the array
package javaproblems;
public class MaximumProduct {
public static void main(String[] args){
int[] arr = {1,2,-3,4,5};
getMaximumNumbers(arr);
}
public static void getMaximumNumbers(int[] arr){
int len = arr.length;
if(len<2){
System.out.println("Invalid Entry");
return;
}
int firstNum = arr[0];
int secondNum = arr[1];
for(int i= 0; i<len; i++){
for(int j=i+1; j<len; j++){
if(arr[i]*arr[j]>firstNum*secondNum){
firstNum = arr[i];
secondNum = arr[j];
}
}
}
System.out.println("Maximum product number is "+firstNum*secondNum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment