HacktoberFestContribute
HacktoberFestContribute copied to clipboard
Find the pair of elements with maximum product in an array.
def maxProduct(arr, n):
if (n < 2):
print("No pairs exists")
return
# Initialize max product pair
a = arr[0]; b = arr[1]
for i in range(0, n):
for j in range(i + 1, n):
if (arr[i] * arr[j] > a * b):
a = arr[i]; b = arr[j]
print("Max product pair is {", a, ",", b, "}",
sep = "")
Driver Code
arr = [1, 4, 3, 6, 7, 0] n = len(arr) maxProduct(arr, n)
"""A Better Solution is to use sorting. Below are detailed steps.
- Sort input array in increasing order.
- If all elements are positive, then return product of last two numbers.
- Else return maximum of products of first two and last two numbers. Time complexity of this solution is O(nLog n). """
I am adding the code in the pull request
@rathoresrikant Can you please assign it to me. I want to contribute its code in c++?
@Kashish05 Please raise Pull Request to contribute. Thanks.