Check whether an array can be fit into another array rearranging the elements in the array | Java Program to check whether an array can be fit into another array with given condition . ; Returns true if the array A can be fit into array B , otherwise false ; Sort both the arrays ; Iterate over the loop and check whether every array element of A is less than or equal to its corresponding array element of B ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static boolean checkFittingArrays ( int [ ] A , int [ ] B , int N ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] > B [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { int A [ ] = { 7 , 5 , 3 , 2 } ; int B [ ] = { 5 , 4 , 8 , 7 } ; int N = A . length ; if ( checkFittingArrays ( A , B , N ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }