We match larger in array_a with smaller in array_b, by which we can have as even sum as possible, so we don't have too small sums.
#include <string> #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <functional> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int> a(n); vector<int> b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end(), std::greater<int>()); bool bBad = false; for (int i = 0; i < n; i++) { if ((a[i] + b[i]) < k) { bBad = true; break; } } cout << (bBad?"NO":"YES") << endl; } return 0; }