Open In App

Distribute values from one Array to another

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:

Below is the code to implement the above approach:




#include <bits/stdc++.h>
using namespace std;
 
int maxChildren(int N, int M, int greed[], int sz[]) {
    // Sort the arrays in ascending order
    sort(greed, greed + N);
    sort(sz, sz + M);
 
    int i = 0;
    int j = 0;
    int count = 0;
 
    // Traverse both arrays and count the number of children that can be satisfied
    while (i < N && j < M) {
        if (sz[j] >= greed[i]) {
            count++;
            i++;
            j++;
        }
        else {
            j++;
        }
    }
 
    return count;
}
 
// Driver's code
int main() {
    int N = 3;
    int M = 2;
    int greed[] = { 1, 2, 3 };
    int sz[] = { 1, 1 };
 
    int result = maxChildren(N, M, greed, sz);
 
    // Function Call
    cout << result << endl;
 
    return 0;
}




// Java code for the above approach
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;
    }
 
    // Driver's code
    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);
 
        // Function Call
        System.out.println(result);
    }
}




def maxChildren(N, M, greed, sz):
    # Sort the arrays in ascending order
    greed.sort()
    sz.sort()
 
    i = 0
    j = 0
    count = 0
 
    # Traverse both arrays and count the number of children that can be satisfied
    while i < N and j < M:
        if sz[j] >= greed[i]:
            count += 1
            i += 1
            j += 1
        else:
            j += 1
 
    return count
 
# Driver's code
if __name__ == '__main__':
    N = 3
    M = 2
    greed = [1, 2, 3]
    sz = [1, 1]
 
    result = maxChildren(N, M, greed, sz)
 
    # Function Call
    print(result)
        
 # This code is contributed by Tushar Rokade




// C# code for the above approach
 
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()
    {
 
        // Code
        int N = 3;
        int M = 2;
        int[] greed = { 1, 2, 3 };
        int[] sz = { 1, 1 };
 
        int result = maxChildren(N, M, greed, sz);
 
        // Function Call
        Console.WriteLine(result);
    }
}
 
// This code is contributed by sankar.




function maxChildren(N, M, greed, sz) {
    // Sort the arrays in ascending order
    greed.sort((a, b) => a - b);
    sz.sort((a, b) => a - b);
 
    let i = 0;
    let j = 0;
    let count = 0;
 
    // Traverse both arrays and count the number of
    // children that can be satisfied
    while (i < N && j < M) {
        if (sz[j] >= greed[i]) {
            count++;
            i++;
            j++;
        } else {
            j++;
        }
    }
 
    return count;
}
 
// Driver's code
    const N = 3;
    const M = 2;
    const greed = [1, 2, 3];
    const sz = [1, 1];
 
    const result = maxChildren(N, M, greed, sz);
 
    // Function Call
    console.log(result);
 
 
// This code is contributed by shivamgupta0987654321

Output
1

Time Complexity: O(max( N*logN, M*logM ))
Auxiliary Space: O(1)


Article Tags :