 # Python Program to find Pair with the Greatest Product in an Array or List

Assume we are given an array with a number of elements our objective is to determine the highest value that is the product of two elements from the given array. If there is no such element, the output should be -1.

Given a list, the task is to find a pair with the greatest Product in the given array or list if the pair is not found then print -1.

Examples:

Example1:

Input:

Given list = [11, 2, 6, 12, 9, 99]

Output:

The Maximum product of pairs in the given list [11, 2, 6, 12, 9, 99] is [ 99 ]

Example2:

Input:

Given list = [11, 19, 24, 2, 48, 96]

Output:

Enter some random List Elements separated by spaces = 11 19 24 2 48 96
The Maximum product of pairs in the given list [11, 19, 24, 2, 48, 96] is [ 96 ]

## Program to find Pair with the Greatest Product in an Array or List in Python

Below are the ways to find a pair with the greatest Product in the given array or list if the pair is not found then print -1.

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

### Method #1: Using For Loop (Static Input)

Approach:

• Give the list as static input and store it in a variable.
• Take a variable maximproduct which stores the maximum product of pairs and initialize it to -1.
• Calculate the length of the given list and store it in a variable lstlngth.
• Iterate till the lstlngth using For loop use the iterator name as m.
• Loop till lstlngth -1 using another For loop (Nested For loop) use the iterator name as n.
• Loop from the second loop iterator value+1 to lstlngth using another For loop(Nested For loop) use the iterator name as p.
• Check if givenlist[n]*givenlist[p] is equal to givenlist[m] using the If statement.
• Using the max function calculate the maximum value of givenlist[m] and maximproduct and store it in maximproduct.
• Print the maximproduct which is the result.
• The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlst = [11, 2, 6, 12, 9, 99]
# Take a variable maximproduct which stores
# the maximum product of pairs and initialize it to -1.
maximproduct = -1
# Calculate the length of the given list and store it in a variable lstlngth.
lstlngth = len(gvnlst)
# Iterate till the lstlngth using For loop use the iterator name as m.

for m in range(lstlngth):
# Loop till lstlngth -1 using another For loop
# (Nested For loop) use the iterator name as n.
for n in range(lstlngth - 1):
# Loop from the second loop iterator value+1 to lstlngth using another For loop
# (Nested For loop) use the iterator name as p.
for p in range(n + 1, lstlngth):
# Check if givenlist[n]*givenlist[p] is equal
# to givenlist[m] using the If statement.
if (gvnlst[n] * gvnlst[p] == gvnlst[m]):
# Using the max function calculate the maximum value of
# givenlist[m] and maximproduct and store it in maximproduct .
maximproduct = max(maximproduct, gvnlst[m])
# Print the maximproduct which is the result.
print('The Maximum product of pairs in the given list',
gvnlst, 'is [', maximproduct, ']')


Output:

The Maximum product of pairs in the given list [11, 2, 6, 12, 9, 99] is [ 99 ]

### Method #2: Using For Loop (User Input)

Approach:

• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Take a variable maximproduct which stores the maximum product of pairs and initialize it to -1.
• Calculate the length of the given list and store it in a variable lstlngth.
• Iterate till the lstlngth using For loop use the iterator name as m.
• Loop till lstlngth -1 using another For loop (Nested For loop) use the iterator name as n.
• Loop from the second loop iterator value+1 to lstlngth using another For loop(Nested For loop) use the iterator name as p.
• Check if givenlist[n]*givenlist[p] is equal to givenlist[m] using the If statement.
• Using the max function calculate the maximum value of givenlist[m] and maximproduct and store it in maximproduct.
• Print the maximproduct which is the result.
• The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Take a variable maximproduct which stores
# the maximum product of pairs and initialize it to -1.
maximproduct = -1
# Calculate the length of the given list and store it in a variable lstlngth.
lstlngth = len(gvnlst)
# Iterate till the lstlngth using For loop use the iterator name as m.

for m in range(lstlngth):
# Loop till lstlngth -1 using another For loop
# (Nested For loop) use the iterator name as n.
for n in range(lstlngth - 1):
# Loop from the second loop iterator value+1 to lstlngth using another For loop
# (Nested For loop) use the iterator name as p.
for p in range(n + 1, lstlngth):
# Check if givenlist[n]*givenlist[p] is equal
# to givenlist[m] using the If statement.
if (gvnlst[n] * gvnlst[p] == gvnlst[m]):
# Using the max function calculate the maximum value of
# givenlist[m] and maximproduct and store it in maximproduct .
maximproduct = max(maximproduct, gvnlst[m])
# Print the maximproduct which is the result.
print('The Maximum product of pairs in the given list',
gvnlst, 'is [', maximproduct, ']')


Output:

Enter some random List Elements separated by spaces = 11 19 24 2 48 96
The Maximum product of pairs in the given list [11, 19, 24, 2, 48, 96] is [ 96 ]

Related Programs: