Question Name:Permutations in array

#include <iostream>
#include<bits/stdc++.h> 
using namespace std; 
  

bool isPossible(int a[], int b[], int n, int k) 
{ 
    sort(a, a + n); 
  
    sort(b, b + n, greater<int>()); 
  
    for (int i = 0; i < n; i++) 
        if (a[i] + b[i] < k) 
            return false; 
  
    return true; 
} 
  
int main() 
{ 
    int a[10]; 
    int b[10]; 
    int k ,i,j,w,l; 
    
  cin>>l;
    for(j=0;j<l;j++ ) { 
cin>>w>>k; int a[w]; 
    int b[w]; 
  for(i=0;i<w;i++ ) { cin>>a[i]; } 
    for(i=0;i<w;i++ ) { cin>>b[i]; } 
    
  
    isPossible(a, b, w, k) ? cout << "1\n" : 
                             cout << "0"; }
    return 0; 
} 


Problem Description

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

  • Test Case 1

    Input (stdin)

    2
    3 10
    2 1 3
    7 8 9
    4 5
    1 2 2 1
    3 3 3 4
    

    Expected Output

    1
    0
  • Test Case 2

    Input (stdin)

    2
    4 5
    2 4 1 5
    4 3 5 7
    3 5
    1 8 5
    2 5 8
    

    Expected Output

    1
    1

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.