Hacker Slotions for Two-arrays


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 arrangement of the arrays, such that, Ai+Bi ≥ K for all i, where Ai denotes the ith element in the array A.




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).




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".





Program


#include<stdio.h>

void ascending(int *a,int size){
    int i,j,t;
    for(i=0;i<size;i++){
        for(j=i+1;j<size;j++){
            if( *(a+i)> *(a+j)){
                t= *(a+i);
                *(a+i)= *(a+j);
                *(a+j)= t;
            }
        }
    }
}
void descending(int *a,int size){
    int i,j,t;
    for(i=0;i<size;i++){
        for(j=i+1;j<size;j++){
            if( *(a+i)< *(a+j)){
                t= *(a+i);
                *(a+i)= *(a+j);
                *(a+j)= t;
            }
        }
    }
}


main (){
    int t,i,j,temp,flag;
    scanf("%d",&t);
    int n,k;
    for(i=0;i<t;i++){
        scanf("%d %d",&n,&k);
        int a[n],b[n];
        for(j=0;j<n;j++){
            scanf("%d",&a[j]);
        }
        for(j=0;j<n;j++){
            scanf("%d",&b[j]);
        }
        ascending(a,n);
        descending(b,n);
        flag=1;
        for(temp=0;temp<n;temp++){
            if((a[temp]+b[temp])<k)
                flag=0;
        }
        if(flag==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
}



No comments:

Post a Comment