Given input integer array arr, find the maximum product of 2 integers that are part of this array.
| Input Parameters |
Expected outputs |
| arr={1,2,3,4}
|
Max product is 4 * 3 = 12
|
| arr={-1,-2,-3,-4}
|
Max product is -4 * -3 = 12
|
| arr={-3,-2,-1,0,1,2,3}
|
Max product is -3 * -2 = 6
|
| The java method should accept following input parameters: arr1 (int array).
|
| Initialize two variables names max and secMax to Integer.MIN.
|
| Initialize two variables named min and secMin to Integer.MAX.
|
| Iterate through arr using for loop and index idx:
If arr[idx]>max:
set secMax=max.
set max=arr[idx].
else if arr[idx] >secMax:
set secMax=arr[idx].
If arr[idx]< min:
set secMin=min.
set min=arr[idx].
o else if arr[idx] < secMin:
set secMin=arr[idx].
|
| If (max*secMax > min*secMin) return max and secMax.
|
| Else return min and secMin.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArrayFindMaxProduct {
public int[] arrayFindMaxProduct(int[] arr) throws Exception{
int max=Integer.MIN_VALUE; int secMax=Integer.MIN_VALUE;
int min=Integer.MAX_VALUE; int secMin=Integer.MAX_VALUE;
for (int idx=0; idx < arr.length; idx++) {
if (arr[idx] > max) {
secMax=max;
max=arr[idx];
}else if (arr[idx] > secMax) {
secMax=arr[idx];
}
if (arr[idx] < min) {
secMin=min;
min=arr[idx];
}else if (arr[idx] < secMin){
secMin=arr[idx];
}
}
int prodMax=max*secMax; int prodMin=min*secMin;
int[] retArr=new int[2];
if (prodMax > prodMin) {
retArr[0]=max; retArr[1]=secMax;
}else {
retArr[0]=min;
retArr[1]=secMin;
}
return retArr;
}
public static void main(String[] args) {
ArrayFindMaxProduct ap=new ArrayFindMaxProduct();
int[] retArr;
try {
int[] intArray1 = {1,2,3,4};
printArraySummary(intArray1, "Original Array");
retArr=ap.arrayFindMaxProduct(intArray1);
System.out.println("Max product is "+retArr[0]+" * "+retArr[1]+" = "+retArr[0]*retArr[1]);
int[] intArray2 = {-1,-2,-3,-4};
printArraySummary(intArray2, "Original Array");
retArr=ap.arrayFindMaxProduct(intArray2);
System.out.println("Max product is "+retArr[0]+" * "+retArr[1]+" = "+retArr[0]*retArr[1]);
int[] intArray3 = {-3,-2,-1,0,1,2,3};
printArraySummary(intArray3, "Original Array");
retArr=ap.arrayFindMaxProduct(intArray3);
System.out.println("Max product is "+retArr[0]+" * "+retArr[1]+" = "+retArr[0]*retArr[1]);
int[] intArray4 = {5,4};
printArraySummary(intArray4, "Original Array");
retArr=ap.arrayFindMaxProduct(intArray4);
System.out.println("Max product is "+retArr[0]+" * "+retArr[1]+" = "+retArr[0]*retArr[1]);
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}