
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Find Two Array Elements Having Maximum Product in Java?
Two elements giving the maximum product in an array means, we have to find two largest array elements which will eventually give the maximum product possible.
In this article we will see how we can find the maximum product of two elements in Java.
To show you some instances
Instance-1
Suppose we have the below array
[10, 2, 3, -5, 99, 12, 0, -1]
In this array the largest element is 99 and second largest is 12.
Max product = 99 * 12
Hence the maximum product of two elements in this array is 1188.
Instance-2
Suppose we have the below array
[6, 1, 2, 4, 3, 5, 9]
In this array the largest element is 9 and the second largest is 6.>
Max product = 9 * 6
Hence the maximum product of two elements in this array is 54.
Instance-3
Suppose we have the below array
[10, 17, 14, 12, 15, 6, 5, 19]
In this array the largest element is 19 and the second largest is 17.
Max product= 19 * 17
Hence the maximum product of two elements in this array is 323.
Algorithm
Algorithm-1
Step 1 − Use a for loop to find the largest and second largest element in the array.
Step 2 − Find their product.
Step 3 − Print the product.
Algorithm-2
Step 1 − Sort the array elements.
Step-2 − Take the last and second last element of the array.
Step 3 −Find their product.
Step 4 −Print the product.
Syntax
To sort the array we need to use the sort( ) method of the Arrays class of java.util package.
Following is the syntax to sort any array in ascending order using the method-
Arrays.sort(array_name);
Where, ‘array_name’ refers to the array that you want to sort.
Multiple Approaches
We have provided the solution in different approaches.
Find The Largest Product Using for Loop.
Find The Largest Product Using Arrays.sort.
Let’s see the program along with its output one by one.
Approach-1: By Using for Loop
In this approach, we use a for loop to iterate through the array elements to find out the largest and second largest element. These two elements will give the maximum product.
Example
public class Main { public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, -5, 99, 12, 0, -1 }; // Storing the first element in both variables int first = arr[0], second = arr[0]; // For loop to iterate the elements from 1 to n // to find the first largest element for (int i = 0; i < arr.length; i++) { // If array element is larger than current largest element, then swap if (arr[i] > first) first = arr[i]; } // For loop to iterate the elements from 1 to n // to find the second largest element for (int i = 0; i < arr.length; i++) { // If array element is larger than current largest element and not equals to // largest element, then swap if (arr[i] > second && arr[i] != first) second = arr[i]; } // Print the product System.out.println("Largest product = " + (first * second)); System.out.println("The elements are " + first + " and " + second); } }
Output
Largest product = 1188 The elements are 99 and 12
Approach-2: By Using Arrays.sort
In this approach, we sort the array using the Arrays.sort( ) method. Then we take the elements at last and second last index. As the array was sorted already, these two elements will give the maximum product.
Example
import java.util.Arrays; public class Main{ public static void main(String[] args) { // The array elements int arr[] = { 10, 2, 3, -5, 9, 12, 0, -1 }; // Sort the array using the sort method from array class Arrays.sort(arr); // Storing the last element as largest and second last element as second largest int first = arr[arr.length - 1], second = arr[arr.length - 2]; // Print the maximum product System.out.println("Maximum product = " + (first * second)); System.out.println("The elements are " + first + " and " + second); } }
Output
Maximum product = 120 The elements are 12 and 10
In this article, we explored different approaches to find two elements in an array with maximum product in Java.
- Related Articles
- Find Two Array Elements Having Maximum Sum in Java?
- Program to find maximum product of two distinct elements from an array in Python
- Maximum product of any two adjacent elements in JavaScript
- Golang program to find the maximum product of two numbers in an array using two pointer approach
- Find sum of two array elements index wise in Java
- 8086 program to determine product of corresponding elements of two array elements
- Find a pair with maximum product in array of Integers in C++
- Count of subsequences having maximum distinct elements in C++
- How to Alter Two Array Elements in Java
- Find a number that divides maximum array elements in C++
- Find k maximum elements of array in original order in C++
- Find the element having different frequency than other array elements in C++
- Maximum product subset of an array in C++
- Maximum product of 4 adjacent elements in matrix in C++
- Maximum Product Subarray - Using Two Traversals in C++
