- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

[6, 1, 2, 4, -3, -4]

72

- Related Questions & Answers
- Program to find the product of three elements when all are unique in Python
- Program to find largest of three numbers - JavaScript
- 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 Product of unique prime factors of a number
- Program to find out the k-th largest product of elements of two arrays in Python
- Largest product of contiguous digits in Python
- Java program to find largest of the three numbers using ternary operators
- Product of unique prime factors of a number in Python Program
- 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
- C/C++ Program to find Product of unique prime factors of a number?
- How to Find the Largest Among Three Numbers using Python?
- Python Program for Product of unique prime factors of a number

Advertisements