Permute two arrays such that sum of every pair is greater or equal to K Easy 0 attempts
GeeksforGeeks ↗

Permute two arrays such that sum of every pair is greater or equal to K

Easy Sorting&Searching GeeksforGeeks

Given two arrays A and B of size N and a number K, check if we can permute them such that A[i]+B[i] >= K for all i.

Example: A=[2,1,3], B=[7,8,9], K=10 → true

Sample Input
Sample Output
Constraints
  • 1 <= N <= 10^5
  • 1 <= A[i], B[i] <= 10^6

Sort + Greedy

Sort A ascending and B descending, check each pair.

function canPermute(A, B, k) {
  A.sort((a,b) => a-b); B.sort((a,b) => b-a);
  return A.every((a,i) => a + B[i] >= k);
}

Time: O(n log n) | Space: O(1)

Saved in this browser only. Private to you.

JavaScript