HackerRank: Two arrays
static void solve()
{
for(int T = ni();T >= 1;T--){
int n = ni(), K = ni();
int[] a = na(n);
int[] b = na(n);
Arrays.sort(a);
Arrays.sort(b);
boolean ok = true;
for(int i = 0;i < n;i++){
if(a[i] + b[n-1-i] >= K){
}else{
ok = false;
}
}
out.println(ok ? "YES" : "NO");
}
}
You are given two integer arrays, A and B, each containing N integers. The size of the array is less than or equal to 1000. You are free to permute the order of the elements in the arrays.
Now here's the real question: Is there an permutation A', B' possible of A and B, such that,A'i+B'i ≥ K for all i, where A'i denotes the ith element in the array A' and B'i denotes ith element in the array B'.
We can arrive at this by sorting one of the arrays in ascending order and the other in descending order and then for every i, check if the condition (A[i] + B[i] >= k ) holds true or not for each of the array indices i. Think
It can be easily seen that, if the condition fails on the sorted arrays, then there exists no permutation of A and B such that the condition holds good.
static void solve()
{
for(int T = ni();T >= 1;T--){
int n = ni(), K = ni();
int[] a = na(n);
int[] b = na(n);
Arrays.sort(a);
Arrays.sort(b);
boolean ok = true;
for(int i = 0;i < n;i++){
if(a[i] + b[n-1-i] >= K){
}else{
ok = false;
}
}
out.println(ok ? "YES" : "NO");
}
}