- Related Questions & Answers
- Program to find the length of longest substring which has two distinct elements in Python
- Python Program to Find the Product of two Numbers Using Recursion
- Find two distinct prime numbers with given product in C++ Program
- 8086 program to determine product of corresponding elements of two array elements
- Program to find the product of three elements when all are unique in Python
- Find two distinct prime numbers with given product in C++
- Python program to find N largest elements from a list
- Program to find a list of product of all elements except the current index in Python
- Largest product of contiguous digits in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Program to find largest distance pair from two list of numbers in Python
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Program to find the sum of largest K sublist in Python
- Program to find the largest sum of the path between two nodes in a binary tree in Python

- 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

Advertisements