# Check whether an array can be fit into another array rearranging the elements in the array

Given two arrays A and B of same size N. Check whether array A can be fit into array B. An array is said to fit into another array if by arranging the elements of both arrays, there exists a solution such that the ith element of the first array is less than or equal to ith element of the second array.

Examples:

```Input : A[] = { 7, 5, 3, 2 }, B[] = { 5, 4, 8, 7 }
Output : YES
Rearrange the first array to {3, 2, 7, 5}
Do not rearrange the second array's element.
After rearranging, all Ai<=Bi.

Input : A[] = { 7, 5, 3, 2, 5, 105, 45, 10 }, B[] = { 2, 4, 0, 5, 6, 9, 75, 84 }
Output : NO```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach : Sort both the arrays and check whether Ai is less than or equal to Bi for all 0 ≤ i ≤ N. If at any ith position Ai is greater than Bi return false, otherwise return true.
Below is the implementation of the above approach:

 `// C++ Program to check whether an array ` `// can be fit into another array with given ` `// condition. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if the array A can be fit into ` `// array B, otherwise false ` `bool` `checkFittingArrays(``int` `A[], ``int` `B[], ``int` `N) ` `{ ` `    ``// Sort both the arrays ` `    ``sort(A, A + N); ` `    ``sort(B, B + N); ` ` `  `    ``// Iterate over the loop and check whether every ` `    ``// array element of A is less than or equal to ` `    ``// its corresponding array element of B ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``if` `(A[i] > B[i]) ` `            ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `A[] = { 7, 5, 3, 2 }; ` `    ``int` `B[] = { 5, 4, 8, 7 }; ` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A); ` ` `  `    ``if` `(checkFittingArrays(A, B, N)) ` `        ``cout << ``"YES"``; ` `    ``else` `        ``cout << ``"NO"``; ` `    ``return` `0; ` `} `

 `// Java Program to check ` `// whether an array can ` `// be fit into another ` `// array with given ` `// condition. ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Returns true if the ` `// array A can be fit  ` `// into array B,  ` `// otherwise false ` `static` `boolean` `checkFittingArrays(``int` `[]A, ` `                                  ``int` `[]B,  ` `                                  ``int` `N) ` `{ ` `     `  `    ``// Sort both the arrays ` `    ``Arrays.sort(A); ` `    ``Arrays.sort(B); ` ` `  `    ``// Iterate over the loop ` `    ``// and check whether every ` `    ``// array element of A is  ` `    ``// less than or equal to ` `    ``// its corresponding array  ` `    ``// element of B ` `    ``for` `(``int` `i = ``0``; i < N; i++) ` `        ``if` `(A[i] > B[i]) ` `            ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `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"``); ` `} ` `} `

 `# Python3 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 ` `def` `checkFittingArrays(A, B, N): ` `     `  `    ``# Sort both the arrays ` `    ``A ``=` `sorted``(A) ` `    ``B ``=` `sorted``(B) ` ` `  `    ``# Iterate over the loop and check whether  ` `    ``# every array element of A is less than  ` `    ``# or equal to its corresponding array  ` `    ``# element of B ` `    ``for` `i ``in` `range``(N): ` `        ``if` `(A[i] > B[i]): ` `            ``return` `False` ` `  `    ``return` `True` ` `  `# Driver Code ` `A ``=` `[``7``, ``5``, ``3``, ``2``] ` `B ``=` `[``5``, ``4``, ``8``, ``7``] ` `N ``=` `len``(A) ` ` `  `if` `(checkFittingArrays(A, B, N)): ` `    ``print``(``"YES"``) ` `else``: ` `    ``print``(``"NO"``) ` ` `  `# This code is contributed  ` `# by mohit kumar `

 `// C# Program to check ` `// whether an array can ` `// be fit into another ` `// array with given ` `// condition. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Returns true if the ` `// array A can be fit  ` `// into array B,  ` `// otherwise false ` `static` `bool` `checkFittingArrays(``int` `[]A, ` `                               ``int` `[]B,  ` `                               ``int` `N) ` `{ ` `     `  `    ``// Sort both the arrays ` `    ``Array.Sort(A); ` `    ``Array.Sort(B); ` ` `  `    ``// Iterate over the loop ` `    ``// and check whether every ` `    ``// array element of A is  ` `    ``// less than or equal to ` `    ``// its corresponding array  ` `    ``// element of B ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``if` `(A[i] > B[i]) ` `            ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main ()  ` `{ ` `    ``int` `[]A = {7, 5, 3, 2}; ` `    ``int` `[]B = {5, 4, 8, 7}; ` `    ``int` `N = A.Length; ` `     `  `    ``if` `(checkFittingArrays(A, B, N)) ` `        ``Console.WriteLine(``"YES"``); ` `    ``else` `        ``Console.WriteLine(``"NO"``); ` `} ` `} ` ` `  `// This code is contributed  ` `// by anuj_67. `

 ` ``\$B``[``\$i``]) ` `            ``return` `false; ` ` `  `    ``return` `true; ` `} ` ` `  `// Driver Code ` `\$A` `= ``array``( 7, 5, 3, 2 ); ` `\$B` `= ``array``( 5, 4, 8, 7 ); ` `\$N` `= ``count``(``\$A``); ` ` `  `if` `(checkFittingArrays(``\$A``, ``\$B``, ``\$N``)) ` `    ``echo` `"YES"``; ` `else` `    ``echo` `"NO"``; ` `     `  `// This code is contributed by shs ` `?> `

Output:
```YES
```

Time Complexity : O(N * logN), where N is the size of array.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, Shashank12, mohit kumar 29

Article Tags :
Practice Tags :