- Related Questions & Answers
- Program to find the length of longest substring which has two distinct elements in Python
- 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
- Python program to find N largest elements from a list
- Find two distinct prime numbers with given product in C++
- Largest product of contiguous digits in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Program to find a list of product of all elements except the current index in Python
- Program to find largest distance pair from two list of numbers in Python
- 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
- Python program to find the size of largest subset of anagram words
- Python program to find the size of the largest subset of anagram words
- Java program to calculate the product of two numbers

- 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