 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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 sum of multiplied numbers in Python
Suppose we have two lists called nums and multipliers. Now consider an operation where we can remove any number from nums and remove any number from multipliers then multiply them together. We must repeat this operation until one of the lists is empty, we have to find the maximum sum of the multiplied numbers.
So, if the input is like nums = [-4, 4, 3] multipliers = [-2, 2], then the output will be 16, as We can match -4 with -2 and 4 with 2 to get -4 * -2 + 4 * 2.
To solve this, we will follow these steps −
- sort the list nums 
- sort the list multipliers 
- res := 0 
-  if size of nums < size of multipliers, then - swap nums and multipliers := multipliers, nums 
 
- n := size of nums 
- m := size of multipliers 
-  for i in range 0 to m - 1, do -  if multipliers[i] <= 0, then - res := res + nums[i] * multipliers[i] 
 
-  otherwise, - res := res + multipliers[i] * nums[n -(m - i)] 
 
 
-  
- return res 
Example
Let us see the following implementation to get better understanding
def solve(nums, multipliers): nums.sort() multipliers.sort() res = 0 if len(nums) < len(multipliers): nums, multipliers = multipliers, nums n, m = len(nums), len(multipliers) for i in range(m): if multipliers[i] <= 0: res += nums[i] * multipliers[i] else: res += multipliers[i] * nums[n - (m - i)] return res nums = [-4, 4, 3] multipliers = [-2, 2] print(solve(nums, multipliers))
Input
[-4, 4, 3], [-2, 2]
Output
16
