- 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

# Program to find maximum product of two distinct elements from an array in Python

Suppose we have a list of numbers called nums, we have to find the largest product of two unique elements.

So, if the input is like nums = [8, -3, 1, -5], then the output will be 15, (-3)*(-5) = 15 which is maximum here.

To solve this, we will follow these steps −

n := size of nums

nums_sort := sort the list nums

max_left := nums_sort[0] * nums_sort[1]

max_right := nums_sort[n-1] * nums_sort[n-2]

ans := maximum of max_left and max_right

return ans

## Example

Let us see the following implementation to get better understanding

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] print(solve(nums))

## Input

[8, -3, 1, -5]

## Output

15

- Related Questions & Answers
- Program to find the largest product of two distinct elements in Python
- Product of non-repeating (distinct) elements in an Array in C++
- 8086 program to determine product of corresponding elements of two array elements
- Maximum product subset of an array in C++ program
- Program to find maximum product of contiguous subarray in Python
- Maximum product of any two adjacent elements in JavaScript
- Program to find maximum XOR with an element from array in Python
- Python program to find Cartesian product of two lists
- Program to find sign of the product of an array using Python
- Find two distinct prime numbers with given product in C++ Program
- Count distinct elements in an array in Python
- Program to find the length of longest substring which has two distinct elements in Python
- Program to find maximum subarray min-product in Python
- PHP program to find missing elements from an array
- Program to find out the k-th largest product of elements of two arrays in Python

Advertisements