Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rathoresrikant opened this issue Oct 27, 2018 · 4 comments
Open

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

rathoresrikant opened this issue Oct 27, 2018 · 4 comments

Comments

@rathoresrikant
Copy link
Owner

No description provided.

@vivekarya99
Copy link

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). """

@Rajat-dey
Copy link
Contributor

I am adding the code in the pull request

@rathoresrikant rathoresrikant changed the title Find the elements with maximum product in an array. Find the pair of elements with maximum product in an array. Oct 27, 2018
koru1130 added a commit to koru1130/HacktoberFestContribute that referenced this issue Oct 28, 2018
rathoresrikant pushed a commit that referenced this issue Oct 29, 2018
@Kashish05
Copy link

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

@rathoresrikant
Copy link
Owner Author

@Kashish05 Please raise Pull Request to contribute. Thanks.

AbhishekGupta2405 added a commit to AbhishekGupta2405/HacktoberFestContribute that referenced this issue Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants