In the previous article, we have seen Java Program to Find Maximum Sum of Two Integers in an Array
In this article we are going to see how we can find minimum product of two integers in an array.
Java Program to Find Minimum Product of Two Integers in an Array
As we have to write a program which will find the minimum product of two integers in an array of integers so our first target should be finding first two smallest numbers from the array elements. As it is very much clear that product of two smaller numbers will give minimum product value than product of two bigger numbers.
For example:
If we have an array say arr=[1, 7, 4, 2, 8, 6, 3, 9, 5] Then in this array first two smaller numbers are 1 and 2. So product is 1*2=2 which is maximum product value we can find than any other possible combinations of array elements.
Let’s see different ways to find minimum product of two integers in an array.
Method-1: Java Program to Find Minimum Product of Two Integers in an Array By Using Sorting Technique
Approach:
- Declare an array and with array elements.
- Sort the array by using
Arrays.sort()
method. - Now array is sorted in ascending order.
- So, find the product of first element and second element of the array which will give the minimum product value.
- Print the result.
Program:
import java.util.Arrays; import java.util.Comparator; class Main { public static void main(String[] args) { //integer array declared along with integer elements Integer[] input = { 1,6,3,4,5 }; // sort the array in ascending order Arrays.sort(input); //as array is sorted in ascending order //so first two elements are smallest elements //so by multiplying the first two elements we will get minimum product long result=input[0]*input[1]; //input[input.length-1] represents last elements //input[input.length-2] second last element System.out.println("Two numbers are "+ input[0] +" and "+ input[1]+" having minimum product "+result); } }
Output: Two numbers are 1 and 3 having minimum product 3
Method-2: Java Program to Find Minimum Product of Two Integers in an Array By Finding First Two Biggest Element
Approach:
- Take user input of length of array.
- Take input of integer array elements.
- Call the user defined method
findSmallest()
to find first 2 small elements in the array sayfirstNumber
,secondNumber
- After getting 2 smallest elements in the array, inside that
findSmallest()
method, callfindMinProduct()
method. - Inside
findMinProduct(
) method addfirstNumber
andsecondNumber
which will give the minimum product value. - Print the result.
Program:
import java.util.Scanner; public class Main { public static void main(String args[]) { int length = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter number of elements in array: "); //array length taken as input length = sc.nextInt(); //array declared int[] arr = new int[length]; //take input of numbers as array element System.out.println("Enter elements into array: "); //taking input of array elements for (int i = 0; i < length; i++) { arr[i] = sc.nextInt(); } System.out.println("First 2 smallest numbers in array : "); //calling findSmallest() method findSmallest(arr); } //user defined method to find first 2 smallest element private static void findSmallest(int[] arr) { //let first two numbers are the smaller numbers int firstNumber=arr[0]; int secondNumber=arr[1]; //loop to find 2 smallest numbers for (int i = 0; i < arr.length; i++) { if (arr[i] < firstNumber) { secondNumber = firstNumber; firstNumber = arr[i]; } else if (arr[i] < secondNumber) { secondNumber = arr[i]; } } System.out.println("First smallest number : " + firstNumber); System.out.println("Second smallest number : " + secondNumber); //calling findMinProduct() method to find minimum product value findMinProduct(firstNumber,secondNumber); } public static void findMinProduct(int firstNumber, int secondNumber) { //multiplying both numbers to find summation value int result= firstNumber*secondNumber; System.out.println("Two numbers are "+ firstNumber +" and "+ secondNumber+" having minimum product "+result); } }
Output: Enter number of elements in array: 5 Enter elements into array: 40 10 30 50 20 First 2 smallest numbers in array : First smallest number : 10 Second smallest number : 20 Two numbers are 10 and 20 having minimum sum 30
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs: