- 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, 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 −

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

[5, 3, 7, 4]

35

- Related Questions & Answers
- 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
- Python program to find Cartesian product of two lists
- Find two distinct prime numbers with given product in C++ Program
- Program to find largest merge of two strings in Python
- Python Program to Find the Product of two Numbers Using Recursion
- Program to find out the dot product of two sparse vectors in Python
- 8086 program to determine product of corresponding elements of two array elements
- Find two distinct prime numbers with given product in C++
- Program to find the product of three elements when all are unique in Python
- Python program to find N largest elements from a list
- Java Program to Find the Product of Two Numbers Using Recursion
- Program to find largest sum of non-adjacent elements of a list in Python

Advertisements