HacktoberFestContribute icon indicating copy to clipboard operation
HacktoberFestContribute copied to clipboard

Find the pair of elements with maximum product in an array.

Open rathoresrikant opened this issue 6 years ago • 4 comments

rathoresrikant avatar Oct 27 '18 18:10 rathoresrikant

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.

  1. Sort input array in increasing order.
  2. If all elements are positive, then return product of last two numbers.
  3. Else return maximum of products of first two and last two numbers. Time complexity of this solution is O(nLog n). """

vivekarya99 avatar Oct 27 '18 18:10 vivekarya99

I am adding the code in the pull request

Rajat-dey avatar Oct 27 '18 18:10 Rajat-dey

@rathoresrikant Can you please assign it to me. I want to contribute its code in c++?

Kashish05 avatar Oct 25 '21 07:10 Kashish05

@Kashish05 Please raise Pull Request to contribute. Thanks.

rathoresrikant avatar Oct 25 '21 09:10 rathoresrikant