Rearrange two given arrays such that sum of same indexed elements lies within given range | C ++ program for the above approach ; Function to check if there exists any arrangements of the arrays such that sum of element lie in the range [ K / 2 , K ] ; Sort the array arr1 [ ] in increasing order ; Sort the array arr2 [ ] in decreasing order ; Traverse the array ; If condition is not satisfied break the loop ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkArrangement ( int A1 [ ] , int A2 [ ] , int n , int k ) { sort ( A1 , A1 + n ) ; sort ( A2 , A2 + n , greater < int > ( ) ) ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( A1 [ i ] + A2 [ i ] > k ) || ( A1 [ i ] + A2 [ i ] < k / 2 ) ) { flag = 1 ; break ; } } if ( flag == 1 ) cout << " No " ; else cout << " Yes " ; } int main ( ) { int arr1 [ ] = { 1 , 3 , 4 , 5 } ; int arr2 [ ] = { 2 , 0 , 1 , 1 } ; int K = 6 ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; checkArrangement ( arr1 , arr2 , N , K ) ; return 0 ; }