Javatpoint Logo
Javatpoint Logo

Find A Pair with Maximum Product in Array of Integers

When working with arrays of integers in Java, there are various scenarios where we might need to find a pair with the maximum product. This task is essential in solving optimization problems, maximizing efficiency, or even finding the largest possible product in a mathematical context. In this section, we will explore different approaches to tackle this problem efficiently using Java programming.

Approach 1: Brute Force Method

The simplest approach to find a pair with the maximum product in an array of integers is to iterate through all possible pairs and calculate their products. We can start with the first element and multiply it with every subsequent element, updating the maximum product whenever we encounter a higher value. This brute force method guarantees finding the pair with the maximum product, but it has a time complexity of O(n^2), making it inefficient for larger arrays.

MaxProductPair.java

Output:

Maximum product pair: -4 and -6
Maximum product: 24

In this code, the findMaxProductPair function iterates through all possible pairs of elements in the array and keeps track of the maximum product found. It returns the pair with the maximum product as an array of two elements. If no such pair is found (e.g., if the array has less than two elements), it returns null.

The time complexity is O(N^2), where N is the number of elements in the array. If the array is large, this approach may not be efficient. There are more optimized approaches that achieve better time complexity, but this brute force solution provides a simple and straightforward implementation for finding the maximum product pair.

Approach 2: Sorting

Another approach involves sorting the array in non-decreasing order. Once the array is sorted, we can consider the last two elements (largest in magnitude) as the potential pair with the maximum product. This approach works well for arrays with positive integers. We can multiply these two numbers to get the maximum product. The time complexity of this approach is O(nlogn) due to the sorting operation.

MaxProductPairSorting.java

Output:

Maximum product pair: -4 and -6
Maximum product: 24

We have an array of integers, let's call it arr.

Our goal is to find a pair of elements from this array that gives the maximum product.

  • We start by sorting the array in ascending order. This rearranges the elements from the smallest to the largest.
  • After sorting the array, the first two elements (arr[0] and arr[1]) will be the smallest two elements, and the last two elements (arr[n - 1] and arr[n - 2]) will be the largest two elements, where n is the size of the array.
  • The product of the smallest two elements will be arr[0] * arr[1], and the product of the largest two elements will be arr[n - 1] * arr[n - 2].
  • To find the pair with the maximum product, we compare the two products obtained in the previous step.
  • The larger of the two products will represent the maximum product pair.

Let's take the same example to illustrate this approach:

Example: Consider the array arr = {2, 3, -4, 5, -6}.

  • Sort the array in ascending order: arr = {-6, -4, 2, 3, 5}.
  • The smallest two elements are -6 and -4, and their product is -6 * -4 = 24.
  • The largest two elements are 3 and 5, and their product is 3 * 5 = 15.
  • Since 24 is greater than 15, the maximum product pair is {-6, -4} with a maximum product of 24.

Approach 3: Optimized Approach

To handle arrays with negative integers, we need to consider an optimized approach. We can find the maximum and minimum elements in the array and check which pair gives the maximum product. To do this, we can iterate through the array and keep track of the maximum and minimum products encountered so far. By comparing the products of the maximum element with the minimum element and vice versa, we can determine the pair with the maximum product.

Here's a Java implementation of the optimized approach:

MaximumProductPair.java

Output:

Maximum product pair: 24

Explanation:

The given array is {1, -2, 3, -4, 5, -6}. The algorithm starts by initializing the maximum product (maxProduct) to the minimum possible value. Then, it iterates through the array to find the maximum and minimum elements.

In each iteration, the algorithm compares the current element (num) with the current maximum (max1) and second maximum (max2). If num is greater than max1, it updates max1 and shifts the previous max1 value to max2. If num is not greater than max1 but greater than max2, it updates max2.

Similarly, the algorithm compares num with the current minimum (min1) and second minimum (min2) elements. If num is smaller than min1, it updates min1 and shifts the previous min1 value to min2. If num is not smaller than min1 but smaller than min2, it updates min2.

After iterating through the entire array, the algorithm calculates the maximum product by comparing the product of the maximum pair (max1 * max2) and the product of the minimum pair (min1 * min2). Finally, it returns the maximum product.

In this case, the maximum product pair is (-2) * (-4) = 8. Hence, the output is Maximum product pair: 8.

Finding a pair with the maximum product in an array of integers is a common problem in Java programming. By applying different approaches, such as the brute force method, sorting, or the optimized approach, we can efficiently solve this problem. Depending on the characteristics of the array, the chosen approach may vary. It is essential to understand the requirements and constraints of the problem to select the most suitable solution.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA