Repository containing solution for #SdeSheetChallenge by striver
You have been given âKâ different arrays/lists, which are sorted individually (in ascending order). You need to merge all the given arrays/list such that the output array/list should be sorted in ascending order.
#include <bits/stdc++.h>
vector<int> mergeTwoSortedArr(vector<int>& a, vector<int>& b)
{
vector<int> res;
int n = a.size();
int m = b.size();
int i = 0, j = 0;
while (i < n && j < m) {
if (a[i] < b[j]) {
res.push_back(a[i]);
i++;
} else {
res.push_back(b[j]);
j++;
}
}
while (i < n) {
res.push_back(a[i]);
i++;
}
while (j < m) {
res.push_back(b[j]);
j++;
}
return res;
}
vector<int> mergeKArrays(vector<vector<int>>& kArr, int i, int j)
{
if (i == j) {
return kArr[i];
}
int mid = (i + j) / 2;
vector<int> left = mergeKArrays(kArr, i, mid);
vector<int> right = mergeKArrays(kArr, mid + 1, j);
return mergeTwoSortedArr(left, right);
}
vector<int> mergeKSortedArrays(vector<vector<int>>& kArrays, int k)
{
return mergeKArrays(kArrays, 0, k - 1);
}
#include <bits/stdc++.h>
vector<int> mergeKSortedArrays(vector<vector<int>>& kArrays, int k)
{
priority_queue<int, vector<int>, greater<int>> pq;
for (auto x : kArrays) {
for (auto y : x) {
pq.push(y);
}
}
vector<int> ans;
while (!pq.empty()) {
ans.push_back(pq.top());
pq.pop();
}
return ans;
}