Given two arrays A of size N and B of size M and an integer K. Create a new array C of size NM* consisting of C[l]= A[i]+B[j] for ≤i≤N, 1≤j≤M,0<=l<=N*M. Find the Kth smallest element in the array C.
What is wrong in this code.
include
include <bits/stdc++.h>
using namespace std;
int fun(int n, int m, int sum, vectora, vectorb) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (sum - a[i] >= 0)
cnt += upper_bound(b.begin(),b.end(), sum - a[i])-b.begin();
else
break;
}
return cnt;
}
int main() {
int t;
cin >> t;
while (t–) {
int n, m, k;
cin >> n >> m >> k;
std::vector<int>a(n),b(m);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < m; i++)
cin >> b[i];
sort(a.begin(),a.end());
sort(b.begin(),b.end());
int l = a[0] + b[0], h = a[n - 1] + b[m - 1], ans;
while (l <= h) {
int sum = (l + h) / 2;
if (fun(n, m, sum, a, b) >= k) {
h = sum - 1;
ans = sum;
}
else
l = sum + 1;
}
cout << ans << endl;
}
return 0;
}