Reduce sum of same | Java program for the above approach ; Function to check if elements of B [ ] can be rearranged such that A [ i ] + B [ i ] <= X ; Checks the given condition ; Sort A [ ] in ascending order ; Sort B [ ] in descending order ; Traverse the arrays A [ ] and B [ ] ; If A [ i ] + B [ i ] exceeds X ; Rearrangement not possible , set flag to false ; If flag is true ; Otherwise ; Driver Code ; Function Call
import java . util . * ; class GFG { static void rearrange ( int A [ ] , int B [ ] , int N , int X ) { boolean flag = true ; Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ N - 1 - i ] > X ) { flag = false ; break ; } } if ( flag == true ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 1 , 2 } ; int X = 4 ; int N = A . length ; rearrange ( A , B , N , X ) ; } }