Hackerrank - Permuting Two Arrays Solution
Consider two -element arrays of integers, and . You want to permute them into some and such that the relation holds for all where . For example, if , , and , a valid satisfying our relation would be and , and .
You are given queries consisting of , , and . For each query, print
YES on a new line if some permutation , satisfying the relation above exists. Otherwise, print
Complete the twoArrays function in the editor below. It should return a string, either
twoArrays has the following parameter(s):
- k: an integer
- A: an array of integers
- B: an array of integers
The first line contains an integer , the number of queries.
The next sets of lines are as follows:
- The first line contains two space-separated integers and , the size of both arrays and , and the relation variable.
- The second line contains space-separated integers .
- The third line contains space-separated integers .
For each query, print
YES on a new line if valid permutations exist. Otherwise, print
2 3 10 2 1 3 7 8 9 4 5 1 2 2 1 3 3 3 4
We perform the following two queries:
, , and . We permute these into and so that the following statements are true:
- Thus, we print
YESon a new line.
- , , and . To permute and into a valid and , we would need at least three numbers in to be greater than ; as this is not the case, we print
NOon a new line.
Solution in Python
def twoArrays(k, A, B): A = sorted(A) B = sorted(B, reverse=True) for x,y in zip(A,B): if x+y<k: return "NO" return "YES" for _ in range(int(input())): n, k = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) print(twoArrays(k, A, B))