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. The task is print “Yes” if any such permutation exists, otherwise print “No”.

Examples:

Input :a[] = {2, 1, 3}, b[] = { 7, 8, 9 }, k = 10.Output :Yes 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 :No

The idea is to sort one array in ascending order and another array in descending order and if any index does not satisfy the condition a[i] + b[i] >= K then print “No”, else print “Yes”.

If the condition fails on sorted arrays, then there exists no permutation of arrays which can satisfy the inequality. **Proof,**

Assume **a _{sort}[]** be sorted a[] in ascending order and

**b**be sorted b[] in descending order.

_{sort}[]Let new permutation b[] is created by swapping any two indices i, j of b

_{sort}[],

**Case 1:**i < j and element at b[i] is now b_{sort}[j].

Now, a_{sort}[i] + b_{sort}[j] < K, because b_{sort}[i] > b_{sort}[j] as b[] is sorted in decreasing order and we know a_{sort}[i] + b_{sort}[i] < k.**Case 2:**i > j and element at b[i] is now b_{sort}[j].

Now, a_{sort}[j] + b_{sort}[i] < k, because a_{sort}[i] > a_{sort}[j] as a[] is sorted in increasing order and we know a_{sort}[i] + b_{sort}[i] < k.

Below is C++ implementation is this approach:

// C++ program to check whether permutation of two // arrays satisfy the condition a[i] + b[i] >= k. #include<bits/stdc++.h> using namespace std; // Check wheather any permutation exists which // satisfy the condition. bool isPossible(int a[], int b[], int n, int k) { // Sort the array a[] in decreasing order. sort(a, a + n); // Sort the array b[] in increasing order. sort(b, b + n, greater<int>()); // Checking condition on each index. for (int i = 0; i < n; i++) if (a[i] + b[i] < k) return false; return true; } // Driven Program int main() { int a[] = { 2, 1, 3 }; int b[] = { 7, 8, 9 }; int k = 10; int n = sizeof(a)/sizeof(a[0]); isPossible(a, b, n, k) ? cout << "Yes" : cout << "No"; return 0; }

Output:

Yes

**Time Complexity:** O(n log n).

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.