# Find pair with greatest product in array in Java

## Introduction:

In computer programming, finding a pair with the greatest product in an array is a common problem that can be solved using various algorithms. The goal of this task is to find the two numbers in the given array whose product is the highest.

## Problem Statement:

Given an array of integers, find two elements such that their product is equal to a given number. If such a pair exists, return the pair of elements, otherwise return the pair not found with the given product.

Example 1:

Consider an array: arr[] = {1, 5, 3, 2}

Possible pairs: (1,5), (1,3), (1,2), (5,3), (5,2), (3,2)

Checking the product of each pair:

(1,5) -> product = 5 (not present in array)

(1,3) -> product = 3 (present in array)

(1,2) -> product = 2 (not present in array)

(5,3) -> product = 15 (not present in array)

(5,2) -> product = 10 (not present in array)

(3,2) -> product = 6 (not present in array)

Therefore, the pair with product present in array is (1,5) and output is (1,5).

Example 2:

Consider an array: arr[] = {3, 7, 2, 9, 10}

Possible pairs: (3,7), (3,2), (3,9), (3,10), (7,2), (7,9), (7,10), (2,9), (2,10), (9,10)

Checking the product of each pair:

(3,7) -> product = 21 (not present in array)

(3,2) -> product = 6 (not present in array)

(3,9) -> product = 27 (not present in array)

(3,10) -> product = 30 (not present in array)

(7,2) -> product = 14 (not present in array)

(7,9) -> product = 63 (not present in array)

(7,10) -> product = 70 (not present in array)

(2,9) -> product = 18 (not present in array)

(2,10) -> product = 20 (present in array)

(9,10) -> product = 90 (not present in array)

Therefore, the pair with product not present in array.

## Approach: Using brute force

The brute-force approach is the most straightforward solution. It iterates over all possible pairs of elements in the array and keeps track of the pair with the greatest product. The pair should be present in arrray.

ALGORITHM:

Step 1: Initialize two integer arrays arr1 and arr2

Step 2: Initialize two boolean flags found1 and found2 to false

Step 3: For each pair of elements in arr1, calculate their product and check if any element in arr1 matches the product

Step 4: If a pair with the greatest product is found in arr1, set found1 to true and break out of the loops

Step 5: Output the pair with the greatest product in arr1 if found1 is true, otherwise output "No pair found in arr1."

Step 6: Repeat steps 3-5 for arr2, checking for pairs with the greatest product

Step 7: Output the pair with the greatest product in arr2 if found2 is true, otherwise output "No pair found in arr2."

FileName: ProductPairBruteForce.java

``````import java.util.*;

public class ProductPairBruteForce {
public static void main(String[] args) {
// Initialize arrays
int[] arr1 = {1, 5, 3, 2};
int[] arr2 = {3, 7, 2, 9, 10};

// Initialize boolean flags to indicate whether a pair has been found in each array
boolean found1 = false;
boolean found2 = false;

// Check for pairs in arr1
for (int i = 0; i < arr1.length; i++) {
for (int j = i + 1; j < arr1.length; j++) {
int prod = arr1[i] * arr1[j];
for (int k = 0; k < arr1.length; k++) {
if (arr1[k] == prod) {
System.out.println("The pair with the greatest product is (" + arr1[i] + "," + arr1[j] + ")");
found1 = true;
break;
}
}
if (found1) {
break;
}
}
if (found1) {
break;
}
}
// Output if no pair was found in arr1
if (!found1) {
System.out.println("No pair found in arr1.");
}

// Check for pairs in arr2
for (int i = 0; i < arr2.length; i++) {
for (int j = i + 1; j < arr2.length; j++) {
int prod = arr2[i] * arr2[j];
for (int k = 0; k < arr2.length; k++) {
if (arr2[k] == prod) {
System.out.println("The pair with the greatest product is (" + arr2[i] + "," + arr2[j] + ")");
found2 = true;
break;
}
}
if (found2) {
break;
}
}
if (found2) {
break;
}
}
// Output if no pair was found in arr2
if (!found2) {
System.out.println("No pair found in arr2.");
}
}
}
``````

Output:

``````The pair with the greatest product is (1,5)
No pair found in arr2.
``````

Complexity Analysis:

Time complexity: The time complexity of this algorithm is O(n^3) because there are three nested loops that each iterate over the length of the input arrays. In the worst case, every element in arr1 and arr2 must be checked for every pair of elements, resulting in n^3 iterations.

Space complexity: The space complexity of this algorithm is O(1) because only a constant amount of additional space is used to store the boolean flags found1 and found2. The input arrays are not modified and do not require additional space.

## Approach: Using Sorting

The sorting approach sorts the array in non-decreasing order and then finds the pair with the greatest product. The pair can be either the first and the last elements of the array or the two largest elements in the array.

ALGORITHM:

Step 1: Initialize the arrays.

Step 2: Sort the arrays in non-decreasing order.

Step 3: Initialize two variables to store the maximum product found in each array.

Step 4: Initialize two pointers to traverse the sorted arrays. One pointer starts from the beginning of the array, and the other pointer starts from the end of the array.

Step 5: While the two pointers have not met:

Step 5.1: Calculate the product of the two elements pointed by the two pointers.

Step 5.2: If the product is greater than the current maximum product, search for the product in the array using a linear search.

Step 5.3: If the product is found in the array and the product is greater than the current maximum product, update the maximum product.

Step 5.4: If the product is not found in the array, move the pointer that points to the smaller element.

Step 6: If the maximum product found is not equal to the minimum integer value, search for the two elements that have the maximum product in the array using two pointers.

Step 7: If the maximum product is found in the array, output the two elements.

FileName: ProductPairSorting.java

``````import java.util.*;

public class ProductPairSorting {
public static void main(String[] args) {
// Initialize arrays
int[] arr1 = {1, 5, 3, 2};
int[] arr2 = {3, 7, 2, 9, 10};

// Check for pairs in arr1
Arrays.sort(arr1); // sort arr1 in ascending order
int maxProd1 = Integer.MIN_VALUE; // set initial maximum product to smallest possible integer value
int n1 = arr1.length; // get length of arr1
int i = 0, j = n1 - 1; // initialize two pointers at the start and end of arr1
while (i < j) {
int prod = arr1[i] * arr1[j]; // calculate product of current pair
if (prod > maxProd1) { // if product is greater than current maximum product
int k = 0;
while (k < n1 && arr1[k] != prod) { // search for a pair in arr1 that has the same product
k++;
}
if (k < n1) { // if a pair with the same product is found
maxProd1 = prod; // set new maximum product to this value
}
}
// move pointer i or j depending on which pair has a higher product
if (arr1[i + 1] * arr1[j] > arr1[i] * arr1[j - 1]) {
i++;
} else {
j--;
}
}
// if a pair with maximum product is found in arr1
if (maxProd1 != Integer.MIN_VALUE) {
int p1 = 0, p2 = n1 - 1; // initialize two pointers at the start and end of arr1
while (p1 < p2 && arr1[p1] * arr1[p2] != maxProd1) { // search for the pair with the maximum product
if (arr1[p1] * arr1[p2] < maxProd1) {
p1++;
} else {
p2--;
}
}
System.out.println("The pair with the greatest product is (" + arr1[p1] + "," + arr1[p2] + ")");
} else { // if no pair with maximum product is found in arr1
System.out.println("No pair found in arr1.");
}

// Check for pairs in arr2
Arrays.sort(arr2); // sort arr2 in ascending order
int maxProd2 = Integer.MIN_VALUE; // set initial maximum product to smallest possible integer value
int n2 = arr2.length; // get length of arr2
i = 0;
j = n2 - 1; // initialize two pointers at the start and end of arr2
while (i < j) {
int prod = arr2[i] * arr2[j]; // calculate product of current pair
if (prod > maxProd2) { // if product is greater than current maximum product
int k = 0;
while (k < n2 && arr2[k] != prod) { // search for a pair in arr2 that has the same product
k++;
}
if (k < n2) { // if a pair with the same product is found
maxProd2 = prod; // set new maximum product to this value
}
}
if (arr2[i + 1] * arr2[j] > arr2[i] * arr2[j - 1]) {
i++;
} else {
j--;
}
}
if (maxProd2 != Integer.MIN_VALUE) {
int p1 = 0, p2 = n2 - 1;
while (p1 < p2 && arr2[p1] * arr2[p2] != maxProd2) {
if (arr2[p1] * arr2[p2] < maxProd2) {
p1++;
} else {
p2--;
}
}
System.out.println("The pair with the greatest product is (" + arr2[p1] + "," + arr2[p2] + ")");
} else {
System.out.println("No pair found in arr2.");
}
}
}
``````

Output:

``````The pair with the greatest product is (1,5)
No pair found in arr2.
``````

Complexity Analysis:

Time complexity: The time complexity of this algorithm is O(n^2 log n) because of the sorting operation. The two nested loops that traverse the sorted array have a time complexity of O(n^2), but the sorting operation has a time complexity of O(n log n).

Space complexity: The space complexity of this algorithm is O(1) because it uses a constant amount of extra memory to store the variables and the arrays.