In the previous article, we have seen Java Program to Check if a Sub Array is Formed by Consecutive Integers from a Given Array of Integers
In this article we are going to see how to Find Maximum Product of Two Integers in an Array of Integers.
Java Program to Find Maximum Product of Two Integers in an Array of Integers
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to Find Maximum Product of Two Integers in an Array of Integers.
Method-1: Java Program to Find Maximum Product of Two Integers in an Array of Integers By Static Initialization of Array Elements
Approach:
- Create two integer variable as max1 and max2 and assign value as -1.
- Iterate the array.
- If current value is more than max1, put current value in max1 and max1 value in max2.
- Else if current value is more than max2, put current value in max2
Program:
public class Main { public static void main(String[] args) { int[] nums = { 3, 2, 7, -1, -4, 9, 5 }; //calling the maxProduct() method maxProduct(nums); } //maxProduct() method to find maximum product of two array elements public static void maxProduct(int[] nums) { int max1 = -1; int max2 = -1; for (int i : nums) { if (i > max1) { max2 = max1; max1 = i; } else if (i > max2) { max2 = i; } } System.out.println("Maximum product is: " + max1 + " * " + max2 + " = " + max1 * max2); } }
Output: Maximum product is: 9 * 7 = 63
Method-2: Java Program to Find Maximum Product of Two Integers in an Array of Integers By Dynamic Initialization of Array Elements
Approach:
- Create scanner class object.
- Ask use length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Create two integer variable as max1 and max2 and assign value as -1.
- Iterate array.
- If current value is more than max1, put current value in max1 and max1 value in max2.
- Else if current value is more than max2, put current value in max2
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the size of array: "); int n = sc.nextInt(); // initialize array with size n int[] arr = new int[n]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } //calling maxProduct() method maxProduct(arr); } //maxProduct() method to find maximum product of two array elements public static void maxProduct(int[] nums) { int max1 = -1; int max2 = -1; for (int i : nums) { if (i > max1) { max2 = max1; max1 = i; } else if (i > max2) { max2 = i; } } System.out.println("Maximum product is: " + max1 + " * " + max2 + " = " + max1 * max2); } }
Output: Enter the size of array: 9 Enter array elements: 1 2 3 4 5 6 7 8 9 Maximum product is: 9 * 8 = 72
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: