Problem Statement
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'.
Input Format
The first line contains an integer, T, the number of test-cases. T test cases follow. Each test case has the following format:
The first line contains two integers, N and K. The second line contains N space separated integers, denoting array A. The third line describes array B in a same format.
Output Format
For each test case, if such an arrangement exists, output "YES", otherwise "NO" (without quotes).
For each test case, if such an arrangement exists, output "YES", otherwise "NO" (without quotes).
Constraints
1 <= T <= 10
1 <= N <= 1000
1 <= K <= 109
0 <= Ai, Bi ≤ 109
Sample Input
2
3 10
2 1 3
7 8 9
4 5
1 2 2 1
3 3 3 4
Sample Output
YES
NO
Explanation
The first input has 3 elements in Array A and Array B, we see that the one of the arrangements, 3 2 1 and 7 8 9 has each pair of elements (3+7, 2 + 8 and 9 + 1) summing upto 10 and hence the answer is "YES".
The second input has array B with three 3s. So, we need at least three numbers in A that are greater than 1. As this is not the case, the answer is "NO".
Copyright © 2015 HackerRank.
All Rights Reserved
All Rights Reserved
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException{
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for( int i = 0 ; i < t ; i++ )
{
String s = br.readLine();
String[] in = s.split(" ");
int n = Integer.parseInt(in[0]);
long k = Integer.parseInt(in[1]);
long[] data1 = new long[n];
long[] data2 = new long[n];
s = br.readLine();
String[] tmp1 = s.split(" ");
s = br.readLine();
String[] tmp2 = s.split(" ");
for( int j = 0 ; j < n ; j ++ )
{
data1[j] = Integer.parseInt(tmp1[j]);
data2[j] = Integer.parseInt(tmp2[j]);
}
Arrays.sort(data1);
Arrays.sort(data2);
int flag = 0;
for( int j = 0 ; j < n ; j++ )
{
if( data1[j] + data2[n-j-1] < k)
{
flag = 1;
break;
}
}
if(flag == 1)
System.out.println("NO");
else
System.out.println("YES");
}
}
}
No comments:
Post a Comment