forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_arrays.cpp
50 lines (47 loc) · 1.34 KB
/
two_arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Given two array A and B
* 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: 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: YES or NO
*
* Solution Approach :
* Greedy Algorithm.
* sort one of the arrays in ascending order and the other in descending order and
* then for every i, check if the condition (A[i] + B[i] >= k ) holds true or not
* for each of the array indices i. It can be deduced that if the condition fails on the sorted arrays,
* then there exists no permutation of A and B such that the condition holds good.
*/
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
int * A = new int[N];
int * B = new int[N];
for ( int i = 0; i < N; ++i) {
cin >> A[i];
}
for(int i = 0; i < N; ++i) {
cin >> B[i];
}
sort(A, A + N);
sort(B, B + N, std::greater<int>()); //decreasing order sort
int j;
for( j = 0; j < N; ++j ) {
if (A[j] + B[j] < K) {
std::cout << "NO" << std::endl;
break;
}
}
if ( j == N ) {
std::cout << "YES" << std::endl;
}
return 0;
}