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 largest product of three unique items in Python
Suppose we have a list of numbers called nums, we have to find the largest product of three unique elements.
So, if the input is like nums = [6, 1, 2, 4, -3, -4], then the output will be 72, as we can multiply (-3) * (-4) * 6 = 72.
Algorithm
To solve this, we will follow these steps ?
Sort the list nums
n := size of nums
maxScore := -infinity
maxScore := maximum of maxScore and (nums[0] * nums[1] * nums[n - 1])
maxScore := maximum of maxScore and (nums[n - 3] * nums[n - 2] * nums[n - 1])
Return maxScore
Why This Approach Works
After sorting, we only need to consider two cases for maximum product ?
Two smallest negatives + largest positive: Two negative numbers multiply to give a positive result
Three largest numbers: If all are positive or mixed signs favor this combination
Example
Let us see the following implementation to get better understanding ?
def solve(nums):
nums.sort()
n = len(nums)
maxScore = float('-inf')
maxScore = max(maxScore, nums[0] * nums[1] * nums[n - 1])
maxScore = max(maxScore, nums[n - 3] * nums[n - 2] * nums[n - 1])
return maxScore
nums = [6, 1, 2, 4, -3, -4]
print(solve(nums))
The output of the above code is ?
72
Step-by-Step Execution
Let's trace through the example ?
nums = [6, 1, 2, 4, -3, -4]
print("Original list:", nums)
nums.sort()
print("Sorted list:", nums)
n = len(nums)
print("Length:", n)
# Case 1: Two smallest * largest
case1 = nums[0] * nums[1] * nums[n - 1]
print(f"Case 1: {nums[0]} * {nums[1]} * {nums[n - 1]} = {case1}")
# Case 2: Three largest
case2 = nums[n - 3] * nums[n - 2] * nums[n - 1]
print(f"Case 2: {nums[n - 3]} * {nums[n - 2]} * {nums[n - 1]} = {case2}")
result = max(case1, case2)
print(f"Maximum product: {result}")
Original list: [6, 1, 2, 4, -3, -4] Sorted list: [-4, -3, 1, 2, 4, 6] Length: 6 Case 1: -4 * -3 * 6 = 72 Case 2: 2 * 4 * 6 = 48 Maximum product: 72
Alternative Approach
We can also solve this without sorting by finding the required elements directly ?
def solve_optimized(nums):
nums.sort() # Still need sorting for this approach
n = len(nums)
# Only two possible maximum products
product1 = nums[0] * nums[1] * nums[-1] # Two smallest * largest
product2 = nums[-3] * nums[-2] * nums[-1] # Three largest
return max(product1, product2)
nums = [6, 1, 2, 4, -3, -4]
print("Result:", solve_optimized(nums))
# Test with different cases
test_cases = [
[1, 2, 3, 4, 5], # All positive
[-5, -4, -3, -2, -1], # All negative
[-2, 0, 1, 2, 3] # Mixed with zero
]
for test in test_cases:
print(f"Input: {test}, Output: {solve_optimized(test)}")
Result: 72 Input: [1, 2, 3, 4, 5], Output: 60 Input: [-5, -4, -3, -2, -1], Output: -6 Input: [-2, 0, 1, 2, 3], Output: 6
Conclusion
This solution efficiently finds the largest product of three unique elements by sorting and comparing only two possible combinations. The time complexity is O(n log n) due to sorting, and space complexity is O(1).
