Translate

Friday, 31 January 2014

Two arrays

You are given two integer arrays, A and B, each containing N integers. The size of the array is <= 1000. You are free to permute the order of the elements in the arrays.
Now for 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 denoting the number of test-cases. T test cases follow. Each test case is given in the following format.
The first line contains two integers, N and K. The second line contains N integers separated by a single space, denoting A array. The third line describes B array in a same format.
Output Format
For each test case, if there is such arrangement exists output “YES”, otherwise “NO” (quotes for clarity).

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 B array with three 3s. So, we need at least three numbers in A to be greater than 1. As its not the case, the answer is “NO”.
............................................................................................................
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int partition(long int A[],long int f,long int l)
{
long int i=f,j,x,temp;
x=A[l];
 for(j=f;j<l;j++)
 {
   if(A[j]<x)
   {
     temp=A[j];
     A[j]=A[i];
     A[i]=temp;
     i++;
   }
 }
    A[l]=A[i];
    A[i]=x;
return i;
}

void Sort1(long int A[],long int f,long int l)
{
 if(f<l)
 {
 long int q=partition(A,f,l);
 Sort1(A,f,q-1);
 Sort1(A,q+1,l);
 }  
}
int main() {
   int T,i;
    scanf("%d",&T);
    for(i=0;i<T;i++)
    {
      int N,i,j,k;
    long int K;
        scanf("%d %ld",&N,&K);
        long int A[N],B[N];
        for(j=0;j<N;j++)
            scanf("%ld",&A[j]);
        for(j=0;j<N;j++)
            scanf("%ld",&B[j]);
        
      Sort1(A,0,N-1);
      Sort1(B,0,N-1);
        k=N;
      for(i=0;i<N;i++,k--)
      {
        if(A[i]+B[k-1]<K)
        {
          printf("NO\n");
          break;
         }
       }
        if(i==N)
       printf("YES\n");
    }    
    
    return 0;
}

No comments:

Post a Comment