Rearrange two given arrays such that sum of same indexed elements lies within given range | Java 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
import java . io . * ; import java . util . * ; class GFG { static void checkArrangement ( Integer [ ] A1 , Integer [ ] A2 , int n , int k ) { Arrays . sort ( A1 ) ; Arrays . sort ( A2 , Collections . reverseOrder ( ) ) ; 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 ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { Integer [ ] arr1 = { 1 , 3 , 4 , 5 } ; Integer [ ] arr2 = { 2 , 0 , 1 , 1 } ; int K = 6 ; int N = arr1 . length ; checkArrangement ( arr1 , arr2 , N , K ) ; } }