- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
To solve this, we will follow these steps −
sort the list nums
n := size of nums
maxScore := -inf
maxScore := maximum of maxScore and (nums * nums * nums[n - 1])
maxScore := maximum of maxScore and (nums[n - 3] * nums[n - 2] * nums[n - 1])
Let us see the following implementation to get better understanding
def solve(nums): nums.sort() n = len(nums) maxScore = -10000 maxScore = max(maxScore, nums * nums * 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))
[6, 1, 2, 4, -3, -4]
- Program to find the product of three elements when all are unique in Python
- Program to find the largest product of two distinct elements in Python
- C++ Program to Find Largest Number Among Three Numbers
- Java Program to Find the Largest Among Three Numbers
- Java Program to find Product of unique prime factors of a number
- Program to find out the k-th largest product of elements of two arrays in Python
- Java program to find largest of the three numbers using ternary operators
- C program to Find the Largest Number Among Three Numbers
- How to Find The Largest Or Smallest Items in Python?
- Largest Unique Number in Python
- Product of unique prime factors of a number in Python Program
- Largest product of contiguous digits in Python
- How to Find the Largest Among Three Numbers using Python?
- C/C++ Program to find Product of unique prime factors of a number?