Distribute values from one Array to another
Last Updated :
16 Aug, 2023
Given N and M which is the size of the array a[] and b[] respectively. You have to distribute each element from array b to array a such that the assigned value to each element in a, should be greater than or equal to the current value in a, the task is to return the maximum number of such valid distributions.
Examples:
Input: N = 3, M = 2, a [ ] = {1, 2, 3}, b [ ] = {1, 1}
Output: 1
Explanation: The values of the array a[] are 1, 2, and 3.
And even though you have 2 values in array b[], since their size is both 1, you could distribute only one element(1) from b to a.
You need to return 1.
Input: N = 2 , M = 3, a [ ] = {1, 2}, b [ ] = {1, 2, 3}
Output: 2
Explanation: The values of the array a[] are 1, 2.
You have 3 values in array b[] and their sizes are big enough to distribute to all of the elements in array b[].
Approach: To solve the problem follow the below idea:
We will use a greedy approach here. We will first sort both arrays in ascending order. Then traverse simultaneously both arrays. If the current element in b[] is larger or equal to the current element in a[], we distribute it and increase the indices of both arrays. If not, move to the next index in b[], and check again. Continue this, until all elements are covered like these from array b[].
Steps that were to follow the above approach:
- Sort the arrays a and b in ascending order.
- Initialize i and j to 0, and count to 0.
- While i is less than N and j is less than M, repeat steps 4 to 6.
- If b[j] is greater than or equal to a[i], increment count and both i and j.
- If not, increment j.
- Repeat previous steps until all elements have been considered from array, b, or a.
- Return count as the answer.
Below is the code to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxChildren( int N, int M, int greed[], int sz[]) {
sort(greed, greed + N);
sort(sz, sz + M);
int i = 0;
int j = 0;
int count = 0;
while (i < N && j < M) {
if (sz[j] >= greed[i]) {
count++;
i++;
j++;
}
else {
j++;
}
}
return count;
}
int main() {
int N = 3;
int M = 2;
int greed[] = { 1, 2, 3 };
int sz[] = { 1, 1 };
int result = maxChildren(N, M, greed, sz);
cout << result << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int maxChildren( int N, int M, int greed[],
int sz[])
{
Arrays.sort(greed);
Arrays.sort(sz);
int i = 0 ;
int j = 0 ;
int count = 0 ;
while (i < N && j < M) {
if (sz[j] >= greed[i]) {
count++;
i++;
j++;
}
else {
j++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 3 ;
int M = 2 ;
int [] greed = { 1 , 2 , 3 };
int [] sz = { 1 , 1 };
int result = maxChildren(N, M, greed, sz);
System.out.println(result);
}
}
|
Python3
def maxChildren(N, M, greed, sz):
greed.sort()
sz.sort()
i = 0
j = 0
count = 0
while i < N and j < M:
if sz[j] > = greed[i]:
count + = 1
i + = 1
j + = 1
else :
j + = 1
return count
if __name__ = = '__main__' :
N = 3
M = 2
greed = [ 1 , 2 , 3 ]
sz = [ 1 , 1 ]
result = maxChildren(N, M, greed, sz)
print (result)
|
C#
using System;
public class GFG {
static int maxChildren( int N, int M, int [] greed,
int [] sz)
{
Array.Sort(greed);
Array.Sort(sz);
int i = 0;
int j = 0;
int count = 0;
while (i < N && j < M) {
if (sz[j] >= greed[i]) {
count++;
i++;
j++;
}
else {
j++;
}
}
return count;
}
static public void Main()
{
int N = 3;
int M = 2;
int [] greed = { 1, 2, 3 };
int [] sz = { 1, 1 };
int result = maxChildren(N, M, greed, sz);
Console.WriteLine(result);
}
}
|
Javascript
function maxChildren(N, M, greed, sz) {
greed.sort((a, b) => a - b);
sz.sort((a, b) => a - b);
let i = 0;
let j = 0;
let count = 0;
while (i < N && j < M) {
if (sz[j] >= greed[i]) {
count++;
i++;
j++;
} else {
j++;
}
}
return count;
}
const N = 3;
const M = 2;
const greed = [1, 2, 3];
const sz = [1, 1];
const result = maxChildren(N, M, greed, sz);
console.log(result);
|
Time Complexity: O(max( N*logN, M*logM ))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...