Home >Backend Development >C++ >Check if an array can fit into another array by rearranging the elements in the array
From the problem description, we can understand that given two arrays, we need to check whether the first array can fit into the second array.
In the real world, there are many situations where we need to check whether an array can fit into another array by rearranging the elements in the array.
For various reasons, the programmer may need to rearrange the items of an array to see if they fit into another array. Memory management in computer programming is one of them. When working with large amounts of data, it is often more efficient to use arrays to store data; however, due to memory limitations, arrays may need to be arranged in a specific way to avoid memory limitations.
Let's try to decode this problem.
Suppose you have two arrays: array A has size n, and array B has size m, where m is greater than or equal to n. The task is to check if it is possible to rearrange the elements of array A such that array A can be completely contained in array B.
In other words, every element of array A must be present in array B and in the same order as in array A. However, there may be additional elements in array B that are not present in array A.
For example, assume that array A contains elements [3,2,1] and array B contains elements [2, 1, 3, 4, 5]. We can rearrange the elements of array A to get [3, 2, 1], which can then be completely contained in array B, as shown below −
On the other hand, if array A contains elements [1, 2, 3] and array B contains elements [2, 3, 4, 5], we cannot rearrange the elements of array A to completely fit into array B because the array There is no element 1 in B.
So, in this case, a function that checks whether array A can fit into array B by rearranging the elements will return False.
Let's decode the entire program into a step-by-step algorithm.
Sort these two arrays in ascending order.
Compares the elements of two arrays, starting with the first entry of each array.
If the element of the smaller array is less than or equal to the corresponding element of the larger array, continue moving to the next element in both arrays.
If the elements of the smaller array are larger than the corresponding elements in the larger array, return "false" because the smaller array cannot fit in the larger array.
Returns "true" if all items of the smaller array are less than or equal to the corresponding elements in the larger array, because the smaller array can fit into the larger array.
Note− Due to the sorting step, the complexity of this algorithm is O(n log n), where n is the size of the array.
C code implementation: Check whether an array can fit into another array by rearranging the elements in the array
#include <iostream> #include <algorithm> #include <vector> using namespace std; bool can_fit(vector<int>& arr_1, vector<int>& arr_2) { //base case if(arr_1.size() > arr_2.size()) return false; // Sort both arrays sort(arr_1.begin(), arr_1.end()); sort(arr_2.begin(), arr_2.end()); // Check if arr_1 can fit into arr_2 int i = 0, j = 0; while (i < arr_1.size() && j < arr_2.size()) { if (arr_1[i] <= arr_2[j]) { i++; j++; } else { return false; } } return true; } int main() { vector<int> A, B; A.push_back(2); A.push_back(5); A.push_back(7); A.push_back(9); A.push_back(10); B.push_back(1); B.push_back(3); B.push_back(5); B.push_back(7); B.push_back(9); B.push_back(9); B.push_back(10); // Check whether B can fit into A if (can_fit(A, B)) { cout << "Array A can fit into array B by rearranging the elements." << endl; } else { cout << "Array A cannot fit into Array B by rearranging the elements." << endl; } return 0; }
Array A cannot fit into array B by rearranging the elements.
Time complexity: O(n log n), because in this code, we first sort the two arrays and then perform an iteration.
Space complexity: O(n), because we store the elements of two vectors in memory.
In this article, we have tried to explain the method of checking whether an array can fit into another array. Hope this article helps you understand this concept better.
The above is the detailed content of Check if an array can fit into another array by rearranging the elements in the array. For more information, please follow other related articles on the PHP Chinese website!