Skip to content
Related Articles

Related Articles

Improve Article

Find the maximum cost of an array of pairs choosing at most K pairs

  • Difficulty Level : Hard
  • Last Updated : 23 Apr, 2019

Given an array of pairs arr[], the task is to find the maximum cost choosing at most K pairs. The cost of an array of pairs is defined as product of the sum of first elements of the selected pair and the minimum among the second elements of the selected pairs. For example, if the following pairs are selected (3, 7), (9, 2) and (2, 5), then the cost will be (3+9+2)*(2) = 28.

Examples:

Input: arr[] = { {4, 7}, {15, 1}, {3, 6}, {6, 8} }, K = 3
Output: 78
The pairs 1, 3 and 4 are selected, therefore the cost = (4 + 3 + 6) * 6 = 78.

Input: arr[] = { {62, 21}, {31, 16}, {19, 2}, {32, 19}, {12, 17} }, K = 4
Output: 2192

Approach: If the second element of a pair is fixed in the answer, then K-1(or less) other pairs are to be selected from those pairs whose second element is greater or equal to the fixed second element and the answer will be maximum if those are chosen such that the sum of first elements is maximum. So, sort the array according to second element and then iterate in descending order taking maximum sum of the first element of K pairs(or less). The maximum sum of first element K pairs can be taken with the help of Set data structure.



Below is the implementation of the above approach:




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Driver function to sort the array elements
// by second element of pairs
bool sortbysec(const pair<int, int>& a,
               const pair<int, int>& b)
{
    return (a.second < b.second);
}
  
// Function that returns the maximum cost of
// an array of pairs choosing at most K pairs.
int maxCost(pair<int, int> a[], int N, int K)
{
    // Initialize result and temporary sum variables
    int res = 0, sum = 0;
  
    // Initialize Set to store K greatest
    // element for maximum sum
    set<pair<int, int> > s;
  
    // Sort array by second element
    sort(a, a + N, sortbysec);
  
    // Iterate in descending order
    for (int i = N - 1; i >= 0; --i) {
        s.insert(make_pair(a[i].first, i));
        sum += a[i].first;
        while (s.size() > K) {
            auto it = s.begin();
            sum -= it->first;
            s.erase(it);
        }
  
        res = max(res, sum * a[i].second);
    }
  
    return res;
}
  
// Driver Code
int main()
{
    pair<int, int> arr[] = { { 12, 3 }, { 62, 21 }, { 31, 16 }, 
                             { 19, 2 }, { 32, 19 }, { 12, 17 }, 
                             { 1, 7 } };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    int K = 3;
  
    cout << maxCost(arr, N, K);
  
    return 0;
}
Output:
2000

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :