- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum Possible Sum of Products in JavaScript
We are given two arrays say, arr1 and arr2 of positive Numbers. The number of values in both the arrays are the same.
We are required to write a function that finds the maximum sum of products of their elements.
Each element in arr1 has to be multiplied with exactly one element in arr2 and vice versa such that each element of both the arrays appears exactly once and the sum of product produced is maximum.
For example: if,
arr1 = [5,1,3,4,2] and, arr2 = [8,10,9,7,6]
Then a possible sum of product is −
5*6 + 1*7 + 3*9 + 4*10 + 2*8
Although, it may not be the largest sum.
Example
Following is the code −
const arr1 = [5,1,3,4,2]; const arr2 = [8,10,9,7,6]; const sorter = (a, b) => b - a; const greatestProduct = (a1, a2) => { if(a1.length !== a2.length){ return false; }; const a1Sorted = a1.slice().sort(sorter); const a2Sorted = a2.slice().sort(sorter); let res = 0; for(let i = 0; i < a1.length; i++){ res += (a1Sorted[i] * a2Sorted[i]); }; return res; }; console.log(greatestProduct(arr1, arr2));
Output
Following is the output in the console −
130
- Related Articles
- Achieving maximum possible pair sum in JavaScript
- Sum of the products of all possible Subsets in C++
- Maximum Sum of Products of Two Arrays in C++
- Maximum Sum of Products of Two Array in C++ Program
- Find maximum sum possible equal sum of three stacks in C++
- Maximum products of two integers in linear time in JavaScript
- C++ program to find maximum possible value of XORed sum
- Maximum contiguous sum of subarray in JavaScript
- Sum of All Possible Odd Length Subarrays in JavaScript
- Subset with maximum sum in JavaScript
- C++ program to find maximum possible value for which XORed sum is maximum
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- Maximum sum of n consecutive elements of array in JavaScript
- C++ program to find maximum possible median of elements whose sum is s
- Find the sum of maximum difference possible from all subset of a given array in Python

Advertisements