# Python program to find pair with the greatest product in an array

In this tutorial we are going to learn about how to find pair with the greatest product in array in Python. Suppose we are given an array which contains a number of elements, our task is to find the greatest number such that it is the product of two elements of the given array. If no such element present, the output should be -1. Elements are within the wide range of 1 to 10^5.

Example1

Input:  a [] = {9, 3, 5, 30, 45}

Output: 45

Explanation: output would be 45 since it is the product of 9 and 5.

Example2

Input:  a [] = {4, 9, 8, 7}

Output:  -1

Explanation: the output would be -1 since no such elements are present.

Example3

Input:  a [] = {9, 2, 4, 5, 35}

Output:-1

Explanation:  the output would be -1 since no such elements are present.

Example4

Input:  a [] = {1, 2, 2, 4, 30, 35}

Output:  4

Explanation: output would be 4 since it is the product of 2 and 2.

Example5

Input: a [] = {19, 21, 1, 35, 30}

Output: -1

Example6

Input: a[] ={2, 4, 8, 3, 19}

Output: 8

## Find pair with the greatest product in an array

Below is the Python code to perform our task:

```def product( a , n):
p=-1
for i in range(n):
for j in range(n - 1):
for k in range(j + 1, n):
if (a[j] * a[k]==a[i]):
p=max(p, a[i])
return p
if __name__ == "__main__":
a=[ 15, 3, 5, 30, 45]
n = len(a)
print(product(a, n))```

Here we are defining a function product and passing the elements of an array and the length of an array then we are traversing the array to find pair with the greatest product in an array and then returning the product and if no such element exists then returning -1.

For example:

Input: a []=[ 15, 3, 5, 30, 45]

Output:45

Explanation:  Output would be 45 since it is the product of 15 and 3.

Output

```45

```

simple approach would be to pick up an element and then check if each pair product is equal to the number and update the maximum number. if the number is maximum, repeat until the whole array gets traversed it generally takes O(n^3) time.

### One response to “Python program to find pair with the greatest product in an array”

1. Hello. This article was extremely interesting, particularly since I was searching for thoughts on this subject last couple of days. Marnia Frederick Alexandrina