Striver-SDE-Sheet

Repository containing solution for #SdeSheetChallenge by striver

View the Project on GitHub

K Max Sum Combinations

You are given two arrays/lists ‘A’ and ‘B’ of size ‘N’ each. You are also given an integer ‘K’. You have to find the ‘K’ maximum and valid sum combinations from all the possible sum combinations of the arrays/lists ‘A’ and ‘B’. Sum combination is made by adding one element from array ‘A’ and another element from array ‘B’.

Priority Queue (TLE)

Code

#include <bits/stdc++.h>
vector<int> kMaxSumCombination(vector<int>& a, vector<int>& b, int n, int k)
{
    priority_queue<int> pq;
    for (auto x : a) {
        for (auto y : b) {
            pq.push(x + y);
        }
    }
    vector<int> ans;
    while (k--) {
        ans.push_back(pq.top());
        pq.pop();
    }
    return ans;
}

Priority Queue (min heap) (TLE)

Code

#include <bits/stdc++.h>
vector<int> kMaxSumCombination(vector<int>& a, vector<int>& b, int n, int k)
{
    priority_queue<int, vector<int>, greater<int>> pq;
    for (auto x : a) {
        for (auto y : b) {
            pq.push(x + y);
            if (pq.size() > k)
                pq.pop();
        }
    }
    vector<int> ans(k, 0);
    for (int i = k - 1; i >= 0; i--) {
        ans[i] = pq.top();
        pq.pop();
    }
    return ans;
}

Optimized Priority Queue

Code

#include <bits/stdc++.h>

vector<int> kMaxSumCombination(vector<int>& a, vector<int>& b, int n, int k)
{
    priority_queue<int, vector<int>, greater<int>> pq;
    for (auto x : a) {
        for (auto y : b) {
            if (pq.size() < k)
                pq.push(x + y);
            else if (x + y > pq.top()) {
                pq.pop();
                pq.push(x + y);
            }
        }
    }
    vector<int> ans(k, 0);
    for (int i = k - 1; i >= 0; i--) {
        ans[i] = pq.top();
        pq.pop();
    }
    return ans;
}