Distribute values from one Array to a different

Given N and M which is the dimensions of the array a[] and b[] respectively. You must distribute every component from array b to array a such that the assigned worth to every component in a, needs to be better than or equal to the present worth in a, the duty is to return the most variety of such legitimate distributions.
Examples:
Enter: N = 3, M = 2, a [ ] = {1, 2, 3}, b [ ] = {1, 1}
Output: 1
Clarification: The values of the array a[] are 1, 2, and three.
And though you will have 2 values in array b[], since their measurement is each 1, you would distribute just one component(1) from b to a.
You want to return 1.Enter: N = 2 , M = 3, a [ ] = {1, 2}, b [ ] = {1, 2, 3}
Output: 2
Clarification: The values of the array a[] are 1, 2.
You have got 3 values in array b[] and their sizes are large enough to distribute to the entire parts in array b[].
Method: To resolve the issue observe the under thought:
We’ll use a grasping strategy right here. We’ll first kind each arrays in ascending order. Then traverse concurrently each arrays. If the present component in b[] is bigger or equal to the present component in a[], we distribute it and improve the indices of each arrays. If not, transfer to the subsequent index in b[], and examine once more. Proceed this, till all parts are coated like these from array b[].
Steps that had been to observe the above strategy:
- Type the arrays a and b in ascending order.
- Initialize i and j to 0, and depend to 0.
- Whereas i is lower than N and j is lower than M, repeat steps 4 to six.
- If b[j] is bigger than or equal to a[i], increment depend and each i and j.
- If not, increment j.
- Repeat earlier steps till all parts have been thought of from array, b, or a.
- Return depend as the reply.
Beneath is the code to implement the above strategy:
Java
|
Time Complexity: O(max( N*logN, M*logM ))
Auxiliary House: O(1)