Find maximum product of two integers in an array

def findMaximumProduct(A):
 
    # to store the maximum and second maximum element in a list
    max1 = A[0]
    max2 = -sys.maxsize
 
    # to store the minimum and second minimum element in a list
    min1 = A[0]
    min2 = sys.maxsize
 
    for i in range(1, len(A)):
 
        # if the current element is more than the maximum element,
        # update the maximum and second maximum element
        if A[i] > max1:
            max2 = max1
            max1 = A[i]
 
        # if the current element is less than the maximum but greater than the
        # second maximum element, update the second maximum element
        elif A[i] > max2:
            max2 = A[i]
 
        # if the current element is more than the minimum element,
        # update the minimum and the second minimum
        if A[i] < min1:
            min2 = min1
            min1 = A[i]
 
        # if the current element is less than the minimum but greater than the
        # second minimum element, update the second minimum element
        elif A[i] < min2:
            min2 = A[i]
 
        # otherwise, ignore the element
 
    # choose the maximum of
    # 1. Product of the maximum and second maximum element or
    # 2. Product of the minimum and second minimum element
    if max1 * max2 > min1 * min2:
        print("Pair is", (max1, max2))
    else:
        print("Pair is", (min1, min2))

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source