Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum product of two distinct elements from an array in Python
Given an array of numbers, we need to find the maximum product of two distinct elements. The key insight is that the maximum product can come from either the two largest numbers or the two smallest numbers (if they are both negative).
For example, if the input is nums = [8, -3, 1, -5], the output will be 15 because (-3) * (-5) = 15, which is the maximum product possible.
Approach
To solve this problem, we follow these steps ?
Sort the array to get elements in ascending order
Calculate the product of the two smallest elements (leftmost)
Calculate the product of the two largest elements (rightmost)
Return the maximum of these two products
Implementation
Here's the complete implementation ?
def solve(nums):
nums_sort = sorted(nums)
max_left = nums_sort[0] * nums_sort[1]
max_right = nums_sort[-1] * nums_sort[-2]
ans = max(max_left, max_right)
return ans
nums = [8, -3, 1, -5]
result = solve(nums)
print(f"Maximum product: {result}")
The output of the above code is ?
Maximum product: 15
How It Works
Let's trace through the example step by step ?
def solve_with_trace(nums):
print(f"Original array: {nums}")
nums_sort = sorted(nums)
print(f"Sorted array: {nums_sort}")
max_left = nums_sort[0] * nums_sort[1]
print(f"Product of two smallest: {nums_sort[0]} * {nums_sort[1]} = {max_left}")
max_right = nums_sort[-1] * nums_sort[-2]
print(f"Product of two largest: {nums_sort[-1]} * {nums_sort[-2]} = {max_right}")
ans = max(max_left, max_right)
print(f"Maximum product: {ans}")
return ans
nums = [8, -3, 1, -5]
solve_with_trace(nums)
Original array: [8, -3, 1, -5] Sorted array: [-5, -3, 1, 8] Product of two smallest: -5 * -3 = 15 Product of two largest: 8 * 1 = 8 Maximum product: 15
Alternative Examples
Let's test with different types of arrays ?
def solve(nums):
nums_sort = sorted(nums)
max_left = nums_sort[0] * nums_sort[1]
max_right = nums_sort[-1] * nums_sort[-2]
return max(max_left, max_right)
# Test cases
test_cases = [
[3, 4, 5, 2], # All positive
[-1, -2, -3, -4], # All negative
[0, 1, 2, 3], # With zero
[-10, -2, 5, 6] # Mixed signs
]
for nums in test_cases:
result = solve(nums)
print(f"Array: {nums} ? Maximum product: {result}")
Array: [3, 4, 5, 2] ? Maximum product: 20 Array: [-1, -2, -3, -4] ? Maximum product: 12 Array: [0, 1, 2, 3] ? Maximum product: 6 Array: [-10, -2, 5, 6] ? Maximum product: 30
Time and Space Complexity
Time Complexity: O(n log n) due to sorting
Space Complexity: O(n) for the sorted array copy
Conclusion
This approach efficiently finds the maximum product by considering that the result comes from either the two smallest or two largest elements after sorting. The algorithm handles both positive and negative numbers correctly by comparing products from both ends of the sorted array.
