# Maximize product of sum of speeds of K workers and their minimum efficiency

• Last Updated : 28 Oct, 2021

Given an integer N, representing the number of workers, an array speed[ ], where speed[i] represents the speed of the ith worker, and an array efficiency[ ], where efficiency[i] represents the efficiency of the ith worker, and an integer K, the task is to select K workers such that the ( Sum of speeds of all the workers ) * ( Minimum efficiency of among K workers ) is maximum possible.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: N = 6, speed[] = {2, 10, 3, 1, 5, 8}, efficiency[] = {5, 4, 3, 9, 7, 2}, K = 2
Output: 60
Explanation:
Selecting 2nd worker (Speed = 10 and Efficiency = 4) and 5th worker ( Speed = 5 and Efficiency = 7).
Therefore, the maximum sum possible = (10 + 5) * min(4, 7) = 60

Input: N = 6, speed[] = {2, 10, 3, 1, 5, 8}, efficiency[] = {5, 4, 3, 9, 7, 2}, K = 3
Output: 68

Approach: Follow the steps below to solve the problem:

• Initialize a vector of pairs arr[ ] where arr[i] equals {efficiency[i], speed[i]} of size N.
• Sort the arr[ ] in decreasing order of efficiency.
• Initialize a min priority_queue that stores the speed of workers.
• Initialize variables say, SumOfSpeed = 0 and Ans = 0.
• Iterate over arr[ ] and do the following:
• Add arr[i].first to the SumOfSpeed
• Push speed[i] in priority_queue.
• If size of the priority_queue exceeds K, the pop the front of the priority_queue and subtract from SumOfSpeed.
• Update Ans as max of Ans and SumOfSpeed * efficiency[i].
• Finally, return the Ans.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to generate array of pairs``void` `generateArrayofPairs(``int` `n, vector<``int``>& speed,``                          ``vector<``int``>& efficiency,``                          ``vector >& arr)``{` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``arr[i] = { efficiency[i], speed[i] };``    ``}` `    ``sort(arr.rbegin(), arr.rend());``}` `// Function to find the maximum``// product of worker speeds and``// their minimum efficiency``int` `maximizePerformance(vector<``int``>& speed, ``int` `K,``                        ``vector<``int``>& efficiency)``{` `    ``int` `n = speed.size();``    ``vector > arr(n);` `    ``// Function to generate``    ``// sorted array of pairs``    ``generateArrayofPairs(n, speed,``                         ``efficiency, arr);` `    ``// Initialize priority queue``    ``priority_queue<``int``, vector<``int``>,``                   ``greater<``int``> >``        ``pq;` `    ``// Initialize ans and sumofspeed``    ``int` `ans = 0;``    ``int` `SumOfSpeed = 0;` `    ``// Traversing the arr of pairs``    ``for` `(``auto``& it : arr) {` `        ``int` `e = it.first;``        ``int` `s = it.second;` `        ``// Updating sum of speed``        ``SumOfSpeed += s;` `        ``// Pushing in priority queue``        ``pq.push(s);` `        ``// If team consists of more than``        ``// K workers``        ``if` `(pq.size() > K) {` `            ``int` `temp = pq.top();``            ``SumOfSpeed -= temp;``            ``pq.pop();``        ``}` `        ``// Taking the maximum performance``        ``// that can be formed``        ``ans = max(ans, SumOfSpeed * e);``    ``}` `    ``// Finally return the ans``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``vector<``int``> speed = { 2, 10, 3, 1, 5, 8 };``    ``vector<``int``> efficiency = { 5, 4, 3, 9, 7, 2 };``    ``int` `K = 2;` `    ``// Function Call``    ``cout << maximizePerformance(``        ``speed, K, efficiency);``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `static` `class` `pair``{``    ``int` `first, second;``     ` `    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}  ``}` `// Function to generate array of pairs``static` `void` `generateArrayofPairs(``int` `n, Vector speed,``                          ``Vector efficiency,``                          ``Vector arr)``{` `    ``for` `(``int` `i = ``0``; i < n; i++) {` `        ``arr.insertElementAt(``new` `pair(efficiency.elementAt(i), speed.elementAt(i)), i);``    ``}` `    ``Collections.sort(arr, ``new` `Comparator() {``            ``@Override` `public` `int` `compare(pair p1, pair p2)``            ``{``                  ``if` `(p1.first != p2.first)``                    ``return` `(p2.first - p1.first);``                  ``return` `p2.second - p1.second;``            ``}``        ``});``}` `// Function to find the maximum``// product of worker speeds and``// their minimum efficiency``static` `int` `maximizePerformance(Vector speed, ``int` `K,``                        ``Vector efficiency)``{` `    ``int` `n = speed.size();``    ``Vector arr = ``new` `Vector<>();` `    ``// Function to generate``    ``// sorted array of pairs``    ``generateArrayofPairs(n, speed,``                         ``efficiency, arr);` `    ``// Initialize priority queue``      ``PriorityQueue pq = ``new` `PriorityQueue();` `    ``// Initialize ans and sumofspeed``    ``int` `ans = ``0``;``    ``int` `SumOfSpeed = ``0``;` `    ``// Traversing the arr of pairs``    ``for` `(``int` `i = ``0``; i < arr.size(); i++) {` `        ``int` `e = arr.elementAt(i).first;``        ``int` `s = arr.elementAt(i).second;` `        ``// Updating sum of speed``        ``SumOfSpeed += s;` `        ``// Pushing in priority queue``        ``pq.add(s);` `        ``// If team consists of more than``        ``// K workers``        ``if` `(pq.size() > K) {` `            ``int` `temp = pq.peek();``            ``SumOfSpeed -= temp;``            ``pq.poll();``        ``}` `        ``// Taking the maximum performance``        ``// that can be formed``        ``ans = Math.max(ans, SumOfSpeed * e);``    ``}` `    ``// Finally return the ans``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main (String[] args) {``    ` `      ``// Given Input``    ``Vector speed = ``new` `Vector();``    ``speed.add(``2``);``      ``speed.add(``10``);``      ``speed.add(``3``);``      ``speed.add(``1``);``      ``speed.add(``5``);``      ``speed.add(``8``);``  ` `    ``Vector efficiency = ``new` `Vector();``      ``efficiency.add(``5``);``      ``efficiency.add(``4``);``      ``efficiency.add(``3``);``      ``efficiency.add(``9``);``      ``efficiency.add(``7``);``      ``efficiency.add(``2``);``  ` `      ``int` `K = ``2``;` `    ``// Function Call``    ``System.out.println(maximizePerformance(``        ``speed, K, efficiency));``}``}` `// This code is contributed by Dharanendra L V.`

## Javascript

 ``

Output:
`60`

Time Complexity: O(NLogN)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up