
- 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 the largest product of two distinct elements in Python
Suppose we have a list of numbers, we have to find the largest product of two distinct elements.
So, if the input is like [5, 3, 7, 4], then the output will be 35
To solve this, we will follow these steps −
- curr_max := -inf
- for i in range 0 to size of nums - 1, do
- for j in range i+1 to size of nums - 1, do
- if nums[i] * nums[j] > curr_max, then
- curr_max := nums[i] * nums[j]
- if nums[i] * nums[j] > curr_max, then
- for j in range i+1 to size of nums - 1, do
- return curr_max
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): curr_max = float('-inf') for i in range(len(nums)): for j in range(i+1, len(nums)): if nums[i] * nums[j] > curr_max: curr_max = nums[i] * nums[j] return curr_max ob = Solution() print(ob.solve([5, 3, 7, 4]))
Input
[5, 3, 7, 4]
Output
35
- Related Articles
- Program to find maximum product of two distinct elements from an array in Python
- Program to find out the k-th largest product of elements of two arrays in Python
- Program to find the length of longest substring which has two distinct elements in Python
- Program to find largest product of three unique items in Python
- Golang Program to find the Distinct elements from two arrays
- Program to find largest merge of two strings in Python
- Find two distinct prime numbers with given product in C++ Program
- Python program to find Cartesian product of two lists
- Python Program to Find the Product of two Numbers Using Recursion
- Python program to find N largest elements from a list
- Program to find largest sum of non-adjacent elements of a list in Python
- Program to find the product of three elements when all are unique in Python
- Program to find out the dot product of two sparse vectors in Python
- Python Program To Find the Smallest and Largest Elements in the Binary Search Tree
- 8086 program to determine product of corresponding elements of two array elements

Advertisements