-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcan_permute_for_sum.cpp
More file actions
32 lines (28 loc) · 1 KB
/
can_permute_for_sum.cpp
File metadata and controls
32 lines (28 loc) · 1 KB
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
/**
* Consider two n-element arrays of integers, A and B.
* You want to permute them into some A' and B' such that the relation
* a_i' + b_i' >= k holds for all i where 0 <= i < n.
* For example, if A = [0, 1], B = [0, 2], and k = 1, a valid A', B'
* satisfying our relation would be A' = [1 , 0] and B' = [0, 2].
* Print YES if some permutations A' and B', exist satisfying the relation
* above. If no valid permutations exist, print NO instead.
*/
#include "can_permute_for_sum.hpp"
#include <algorithm>
#include <map>
using namespace std;
bool can_permute_for_sum(vector<int> const& a, vector<int> const& b, int k) {
if (a.size() != b.size()) {
return false;
}
vector<int> sorted_a = a;
sort(sorted_a.begin(), sorted_a.end());
vector<int> sorted_b = b;
sort(sorted_b.begin(), sorted_b.end(), greater<int>());
for (size_t index = 0; index < a.size(); ++index) {
if (sorted_a[index] + sorted_b[index] < k) {
return false;
}
}
return true;
}