Given two arrays a[] and b[] of equal size and an integer k, Determine whether there exists any permutation of the arrays such that for every index i, the condition a[i] + b[i] ≥ k holds. Return true if such a permutation exists, otherwise return false.
Examples :
Input: a[] = [2, 1, 3], b[] = [7, 8, 9], k = 10
Output: true
Explanation: Rearranging gives a = [1, 2, 3] and b = [9, 8, 7]. Each pair sum (10, 10, 10) is ≥ k = 10Input: a[] = [1, 2, 2, 1], b[] = [3, 3, 3, 4], k = 5
Output: false
Explanation: Even after rearranging both arrays, at least one pair sum becomes less than k = 5.
Hence, no valid permutation exists, and the answer is false.
[Approach] Using Greedy - O(n log(n)) Time and O(1) Space
The idea is to maximize each pair’s sum efficiently by pairing the smallest element of one array with the largest element of the other.
- Sort array a[] in ascending order.
- Sort array b[] in descending order.
Now, for every index i, check whether a[i] + b[i] ≥ k. If this condition is true for all pairs, then it’s possible to form a valid permutation return true. If any pair fails to satisfy the condition, return false immediately.
This greedy strategy works because pairing the smallest elements of a[] with the largest elements of b[] ensures that each sum is as large as possible, giving the best chance for all pairs to meet or exceed k.
//Driver Code Starts
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//Driver Code Ends
bool isPossible(vector<int>& a, vector<int>& b, int k) {
// Sort a[] in ascending order
sort(a.begin(), a.end());
// Sort b[] in descending order
sort(b.begin(), b.end(), greater<int>());
// Check if every pair sum >= k
for (int i = 0; i < a.size(); i++) {
if (a[i] + b[i] < k)
return false;
}
return true;
}
//Driver Code Starts
int main() {
vector<int> a = {2, 1, 3};
vector<int> b = {7, 8, 9};
int k = 10;
if (isPossible(a, b, k))
cout << "true";
else
cout << "false";
return 0;
}
//Driver Code Ends
//Driver Code Starts
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//Driver Code Ends
// Compare function for ascending order
int compareAsc(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
// Compare function for descending order
int compareDesc(const void *a, const void *b) {
return (*(int *)b - *(int *)a);
}
// Function to check if permutation satisfying the condition exists
bool isPossible(int a[], int b[], int n, int k) {
// Sort a[] in ascending order
qsort(a, n, sizeof(int), compareAsc);
// Sort b[] in descending order
qsort(b, n, sizeof(int), compareDesc);
// Check if every pair sum >= k
for (int i = 0; i < n; i++) {
if (a[i] + b[i] < k)
return false;
}
return true;
}
//Driver Code Starts
int main() {
int a[] = {2, 1, 3};
int b[] = {7, 8, 9};
int n = sizeof(a) / sizeof(a[0]);
int k = 10;
if (isPossible(a, b, n, k))
printf("true");
else
printf("false");
return 0;
}
//Driver Code Ends
//Driver Code Starts
import java.util.Arrays;
class GFG {
//Driver Code Ends
static boolean isPossible(int[] a, int[] b, int k) {
// Sort a[] in ascending order
Arrays.sort(a);
// Sort b[] in descending order
Arrays.sort(b);
for (int i = 0; i < b.length / 2; i++) {
int temp = b[i];
b[i] = b[b.length - 1 - i];
b[b.length - 1 - i] = temp;
}
// Check if every pair sum >= k
for (int i = 0; i < a.length; i++) {
if (a[i] + b[i] < k)
return false;
}
return true;
}
//Driver Code Starts
public static void main(String[] args) {
int[] a = {2, 1, 3};
int[] b = {7, 8, 9};
int k = 10;
if (isPossible(a, b, k))
System.out.print("true");
else
System.out.print("false");
}
}
//Driver Code Ends
def isPossible(a, b, k):
# Sort a[] in ascending order
a.sort()
# Sort b[] in descending order
b.sort(reverse=True)
# Check if every pair sum >= k
for i in range(len(a)):
if a[i] + b[i] < k:
return False
return True
if __name__ == '__main__':
#Driver Code Starts
a = [2, 1, 3]
b = [7, 8, 9]
k = 10
if isPossible(a, b, k):
print("true")
else:
print("false")
#Driver Code Ends
//Driver Code Starts
using System;
class GFG {
//Driver Code Ends
static bool isPossible(int[] a, int[] b, int k) {
// Sort a[] in ascending order
Array.Sort(a);
// Sort b[] in descending order
Array.Sort(b);
Array.Reverse(b);
// Check if every pair sum >= k
for (int i = 0; i < a.Length; i++) {
if (a[i] + b[i] < k)
return false;
}
return true;
}
//Driver Code Starts
static void Main() {
int[] a = {2, 1, 3};
int[] b = {7, 8, 9};
int k = 10;
if (isPossible(a, b, k))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
//Driver Code Ends
function isPossible(a, b, k) {
// Sort a[] in ascending order
a.sort((x, y) => x - y);
// Sort b[] in descending order
b.sort((x, y) => y - x);
// Check if every pair sum >= k
for (let i = 0; i < a.length; i++) {
if (a[i] + b[i] < k)
return false;
}
return true;
}
// Driver Code
//Driver Code Starts
let a = [2, 1, 3];
let b = [7, 8, 9];
let k = 10;
if (isPossible(a, b, k))
console.log("true");
else
console.log("false");
//Driver Code Ends
Output
true