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 the implementation is this approach:

## C++

`// 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 whether 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check whether ` `// permutation of two arrays satisfy ` `// the condition a[i] + b[i] >= k. ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `// Check whether any permutation ` `// exists which satisfy the condition. ` `static` `boolean` `isPossible(Integer a[], ` `int` `b[], ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Sort the array a[] in decreasing order. ` ` ` `Arrays.sort(a, Collections.reverseOrder()); ` ` ` ` ` `// Sort the array b[] in increasing order. ` ` ` `Arrays.sort(b); ` ` ` ` ` `// Checking condition on each index. ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `if` `(a[i] + b[i] < k) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) { ` ` ` `Integer a[] = {` `2` `, ` `1` `, ` `3` `}; ` ` ` `int` `b[] = {` `7` `, ` `8` `, ` `9` `}; ` ` ` `int` `k = ` `10` `; ` ` ` `int` `n = a.length; ` ` ` ` ` `if` `(isPossible(a, b, n, k)) ` ` ` `System.out.print(` `"Yes"` `); ` ` ` `else` ` ` `System.out.print(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to check ` `# whether permutation of two ` `# arrays satisfy the condition ` `# a[i] + b[i] >= k. ` ` ` `# Check whether any ` `# permutation exists which ` `# satisfy the condition. ` `def` `isPossible(a,b,n,k): ` ` ` ` ` `# Sort the array a[] ` ` ` `# in decreasing order. ` ` ` `a.sort(reverse` `=` `True` `) ` ` ` ` ` `# Sort the array b[] ` ` ` `# in increasing order. ` ` ` `b.sort() ` ` ` ` ` `# Checking condition ` ` ` `# on each index. ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(a[i] ` `+` `b[i] < k): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` ` ` `# Driver code ` ` ` `a ` `=` `[ ` `2` `, ` `1` `, ` `3` `] ` `b ` `=` `[` `7` `, ` `8` `, ` `9` `] ` `k ` `=` `10` `n ` `=` `len` `(a) ` ` ` `if` `(isPossible(a, b, n, k)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check whether ` `// permutation of two arrays satisfy ` `// the condition a[i] + b[i] >= k. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Check whether any permutation ` `// exists which satisfy the condition. ` `static` `bool` `isPossible(` `int` `[]a, ` `int` `[]b, ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Sort the array a[] ` ` ` `// in decreasing order. ` ` ` `Array.Sort(a); ` ` ` ` ` `// Sort the array b[] ` ` ` `// in increasing order. ` ` ` `Array.Reverse(b); ` ` ` ` ` `// Checking condition on each index. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(a[i] + b[i] < k) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]a = {2, 1, 3}; ` ` ` `int` `[]b = {7, 8, 9}; ` ` ` `int` `k = 10; ` ` ` `int` `n = a.Length; ` ` ` ` ` `if` `(isPossible(a, b, n, k)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check whether ` `// permutation of two arrays satisfy ` `// the condition a[i] + b[i] >= k. ` ` ` `// Check whether any permutation ` `// exists which satisfy the condition. ` `function` `isPossible( ` `$a` `, ` `$b` `, ` `$n` `, ` `$k` `) ` `{ ` ` ` ` ` `// Sort the array a[] in ` ` ` `// decreasing order. ` ` ` `sort(` `$a` `); ` ` ` ` ` `// Sort the array b[] in ` ` ` `// increasing order. ` ` ` `rsort(` `$b` `); ` ` ` ` ` `// Checking condition on each ` ` ` `// index. ` ` ` `for` `( ` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$a` `[` `$i` `] + ` `$b` `[` `$i` `] < ` `$k` `) ` ` ` `return` `false; ` ` ` ` ` `return` `true; ` `} ` ` ` `// Driven Program ` ` ` `$a` `= ` `array` `( 2, 1, 3 ); ` ` ` `$b` `= ` `array` `( 7, 8, 9 ); ` ` ` `$k` `= 10; ` ` ` `$n` `= ` `count` `(` `$a` `); ` ` ` ` ` `if` `(isPossible(` `$a` `, ` `$b` `, ` `$n` `, ` `$k` `)) ` ` ` `echo` `"Yes"` `; ` ` ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed by ` `// anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Pair formation such that maximum pair sum is minimized
- Find a pair of elements swapping which makes sum of two arrays same
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Smallest Difference pair of values between two unsorted Arrays
- Reorder an array such that sum of left half is not equal to sum of right half
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Split Array into min number of subsets with difference between each pair greater than 1
- Find original numbers from gcd() every pair
- Check if two arrays are equal or not
- Check if two arrays can be made equal by reversing subarrays multiple times
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Count quadruples from four sorted arrays whose sum is equal to a given value x
- Find three element from given three arrays such that their sum is X | Set 2
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find Partition Line such that sum of values on left and right is equal
- Noble integers in an array (count of greater elements is equal to value)
- Rearrange array such that even positioned are greater than odd