Check whether an array can be fit into another array rearranging the elements in the array
Last Updated :
01 Mar, 2023
Given two arrays A and B of the 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
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.
Steps to solve the problem:
- Sort array A in non-decreasing order.
- Sort array B in non-decreasing order.
- For each element i from 0 to N-1 do the following:
- If A[i] > B[i], return false.
- If the loop completes without returning false, return true.
Below is the implementation of the above approach:
C++
// C++ Program to check whether an array
// can be fit into another array with given
// condition.
#include <bits/stdc++.h>
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[0]);
if (checkFittingArrays(A, B, N))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// 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
# 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#
// 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.
PHP
<?php
// PHP 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
function checkFittingArrays($A, $B, $N)
{
// Sort both the arrays
sort($A);
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 ($i = 0; $i < $N; $i++)
if ($A[$i] > $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
?>
JavaScript
<script>
// Javascript 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
function checkFittingArrays(A, B, N)
{
// Sort both the arrays
A.sort(function(a, b){return a - b;});
B.sort(function(a, b){return a - 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 (let i = 0; i < N; i++)
if (A[i] > B[i])
return false;
return true;
}
// Driver Code
let A = [7, 5, 3, 2];
let B = [5, 4, 8, 7];
let N = A.length;
if (checkFittingArrays(A, B, N))
document.write("YES");
else
document.write("NO");
// This code is contributed by unknown2108
</script>
Time Complexity: O(N * logN), where N is the size of the array.
Auxiliary Space: O(1)
Similar Reads
Check if an array arr[] can be rearranged such that arr[2 Ã i + 1] = 2* arr[2 Ã i] for every ith index Given an array arr[] consisting of 2*N integers, the task is to check if it is possible to rearrange the array elements such that arr[2 * i + 1] = 2* arr[2 * i] for every ith index. If it is possible to do so, then print "Yes. Otherwise, print "No". Examples: Input: arr[] = {4, -2, 2, -4}, N = 2Outp
7 min read
Check if an array can be converted to another given array by swapping pairs of unequal elements Given two arrays arr1[] and arr2[] of size N, consisting of binary integers, the task is to check if arr1[] can be converted to arr2[] by swapping any pair of array elements (arr1[i], arr1[j]) such that i < j and arr1[i] is 1 and arr1[j] is 0 (any number of times). If it is possible to do so, the
11 min read
Check if Array can be rearranged such that arr[i] XOR arr[i+2] is 0 Given an array arr[] of size N, the task is to check if the array elements can be rearranged in a way such that the bitwise XOR of ith and (i+2)th element is always 0 for any value of i (0 ⤠i < N-2) Examples: Input: arr[] = {1, 1, 2, 2}, N = 4Output: YESExplanation: Rearrange the array like {1,
8 min read
Rearrange an array to make similar indexed elements different from that of another array Given two sorted arrays A[] and B[] consisting of N distinct integers, the task is to rearrange the elements of array B[] such that, for every ith index, A[i] is not equal to B[i]. If multiple such arrangements exist, print any one of them. If no such arrangement exists, print -1. Examples: Input: A
7 min read
Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index Given two arrays A[] and B[], both consisting of a permutation of first N natural numbers, the task is to count the minimum number of times the last array element is required to be shifted to any arbitrary position in the array A[] to make both the arrays A[] and B[] equal. Examples: Input: A[] = {1
6 min read