Permute two arrays such that sum of every pair is greater or equal to K
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 to 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 that can satisfy the inequality. Proof,
Assume asort[] be sorted a[] in ascending order and bsort[] be sorted b[] in descending order.
Let new permutation b[] is created by swapping any two indices i, j of bsort[],
- Case 1: i < j and element at b[i] is now bsort[j].
Now, asort[i] + bsort[j] < K, because bsort[i] > bsort[j] as b[] is sorted in decreasing order and we know asort[i] + bsort[i] < k. - Case 2: i > j and element at b[i] is now bsort[j].
Now, asort[j] + bsort[i] < k, because asort[i] > asort[j] as a[] is sorted in increasing order and we know asort[i] + bsort[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; } // This code is contributed by Aditya Kumar (adityakumar129) |
C
// C program to check whether permutation of two // arrays satisfy the condition a[i] + b[i] >= k. #include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Compare function for qsort for Increasing Order int cmpfunc1( const void * a, const void * b) { return (*( int *)a - *( int *)b); } // Compare function for qsort for decreasing Order int cmpfunc2( const void * a, const void * b) { return (*( int *)b - *( int *)a); } // 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. qsort (a, n, sizeof ( int ), cmpfunc1); // Sort the array b[] in increasing order. qsort (b, n, sizeof ( int ), cmpfunc2); // 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) ? printf ( "Yes" ) : printf ( "No" ); return 0; } // This code is contributed by Aditya Kumar (adityakumar129) |
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. |
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. |
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. |
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. ?> |
Javascript
<script> // JavaScript 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. a.sort( function (a, b){ return a - b}); // Sort the array b[] // in increasing order. b.reverse(); // Checking condition on each index. for (let i = 0; i < n; i++) if (a[i] + b[i] < k) return false ; return true ; } let a = [2, 1, 3]; let b = [7, 8, 9]; let k = 10; let n = a.length; if (isPossible(a, b, n, k)) document.write( "Yes" ); else document.write( "No" ); </script> |
Yes
Time Complexity: O(n log n).
Auxiliary Space: O(1)
This approach is contributed by Anuj Chauhan.
Method 2:
In the above case, we were just sorting the elements and checking the statement for n time (making pairs of ith index) resulting in time complexity of O(n log n).
As we can see we only need to check that is there any pair whose sum is greater than K. To solve it we have to just find the maximum pair sum.
Follow the steps below to implement the above idea:
- We will first find the max element from the first array and then the max element from the second array.
- Check if the sum of both max elements is greater than K
- If true, return true
- otherwise false.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h> using namespace std; bool isPossible( long long arr1[] , long long arr2[] , int n , long long K){ //FINDING THE MAX ELEMENT long long a = *max_element(arr1 , arr1+n); // *max_element is STL function used to find the max element from the array. long long b = *max_element(arr2 , arr2+n); long long ans = a+b; // CHECKING THE CONDITION return ans >= K; } int main(){ long long arr1[] = {2,1,3}; long long arr2[] = {7,8,9}; long long k = 5; int n = sizeof (arr1)/ sizeof (arr1[0]); if (isPossible(arr1 , arr2 , n , k)) { cout<< "Yes" <<endl; } else { cout<< "No" <<endl; } } // This code is contributed by Divyansh Garg (dishugarg1511) |
Java
// Java code for the above approach import java.io.*; import java.util.*; class GFG { static boolean isPossible( int [] arr1, int [] arr2, int n, int K) { int a = Arrays.stream(arr1).max().getAsInt(); int b = Arrays.stream(arr2).max().getAsInt(); int ans = a + b; // CHECKING THE CONDITION return ans >= K; } public static void main(String[] args) { int [] arr1 = { 2 , 1 , 3 }; int [] arr2 = { 7 , 8 , 9 }; int k = 5 ; int n = arr1.length; if (isPossible(arr1, arr2, n, k)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } // This code is contributed by lokesh |
Python3
def isPossible(arr1, arr2, n, K): # Finding the max element a = max (arr1) b = max (arr2) ans = a + b # Checking the condition return ans > = K arr1 = [ 2 , 1 , 3 ] arr2 = [ 7 , 8 , 9 ] k = 5 n = len (arr1) if isPossible(arr1, arr2, n, k): print ( "Yes" ) else : print ( "No" ) #This code is contributed by Narasinga Nikhil |
C#
// C# Program for the above approach. using System; using System.Linq; class Program { static bool IsPossible( long [] arr1, long [] arr2, int n, long K) { // FINDING THE MAX ELEMENT long a = arr1.Max(); long b = arr2.Max(); long ans = a + b; // CHECKING THE CONDITION return ans >= K; } static void Main( string [] args) { long [] arr1 = { 2, 1, 3 }; long [] arr2 = { 7, 8, 9 }; long k = 5; int n = arr1.Length; if (IsPossible(arr1, arr2, n, k)) { Console.WriteLine( "Yes" ); } else { Console.WriteLine( "No" ); } } } |
Javascript
// JavaScript code for the above approach function isPossible(arr1, arr2, n, K) { let a = Math.max(...arr1); let b = Math.max(...arr2); let ans = a + b; // CHECKING THE CONDITION return ans >= K; } let arr1 = [2, 1, 3]; let arr2 = [7, 8, 9]; let k = 5; let n = arr1.length; if (isPossible(arr1, arr2, n, k)) { console.log( "Yes" ); } else { console.log( "No" ); } // This code is contributed by karthik. |
Yes
Time Complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...