
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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[0] * nums[1] * nums[n - 1])
maxScore := maximum of maxScore and (nums[n - 3] * nums[n - 2] * nums[n - 1])
return maxScore
Example
Let us see the following implementation to get better understanding
def solve(nums): nums.sort() n = len(nums) maxScore = -10000 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))
Input
[6, 1, 2, 4, -3, -4]
Output
72
- Related Articles
- 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
- Program to find largest of three numbers - JavaScript
- How to Find The Largest Or Smallest Items in Python?
- Program to find out the k-th largest product of elements of two arrays in Python
- Largest Unique Number in Python
- Product of unique prime factors of a number in Python Program
- Java Program to find Product of unique prime factors of a number
- C++ Program to Find Largest Number Among Three Numbers
- Java Program to Find the Largest Among Three Numbers
- Swift Program to Find the Largest Among Three Numbers
- Haskell program to find the largest among three numbers
- Kotlin Program to Find the Largest Among Three Numbers
- Program to find sum of unique elements in Python
- Python Program for Product of unique prime factors of a number

Advertisements