Permutations in array

PROBLEM :
Given two arrays of equal size n and an integer k. The task is to permute both arrays such that sum of their corresponding element is greater than or equal to k i.e A[i] + B[i] >= k.

Examples:

Input : A[] = {2, 1, 3},
        B[] = { 7, 8, 9 },
        k = 10.
Output : 1
Permutation  A[] = { 1, 2, 3 } and B[] = { 9, 8, 7 }
satisfied the condition A[i] + B[i] >= K.

Input : A[] = {1, 2, 2, 1},
        B[] = { 3, 3, 3, 4 },
        k = 5.
Output : 0

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains three lines.The first line of input contains two integers n and k . Then in the next two lines are space separated values of the array A and B.

Output:
For each test case in a new  line print the required answer.

Constraints:
1<=T<=100
1<=n,k<=200

Example:
Input:
2
3 10
2 1 3
7 8 9
4 5
1 2 2 1
3 3 3 4

Output:
1
0

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<algorithm>
#define max 200
int main()
{
int arr1[max],arr2[max] ;
int n,k,t,i ;
cin>>t  ;
while(t--)
{
   cin>>n>>k ;
   for(i=0;i<n;i++)
       cin>>arr1[i] ;
   for(i=0;i<n;i++)
       cin>>arr2[i] ;
     
   sort(arr1,arr1+n) ;
   sort(arr2,arr2+n,greater<int>()) ;
 
   for(i=0;i<n;i++)
   {
       if(arr1[i]+arr2[i]<k)
           break ;
   }
 
   if(i==n)
       cout<<1;
   else
       cout<<0 ;
   cout<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )