Horje
Divide maximum element in N groups so that received and required has difference atmost K

Given two arrays A[] and B[] of size N and M respectively denoting the required elements of each group and the number of elements in a pack respectively. The task is to distribute the maximum number of packs such that the difference in the number of elements received by a group and their requirement is at most K.

Examples:

Input: N = 4, M = 3, K = 5, A[] = [60, 45, 80, 60], B[] = [30, 60, 75]
Output: 2
Explanation: The 2 packs that will be distributed are as follows.
The pack whose size is 60 is distributed among the group with
requirement 60 as 60 – 5 < 60 and 60 + 5 > 60 .
The pack whose size is 75 is distributed among the group with
requirement 80 as 80 – 5 = 75 and 80 + 5 > 75.

Input: N = 4, M = 3, K = 10, A[] = [60, 40, 80, 60], B[] = [30, 60, 75]
Output: 3
Explanation: All the packs will be distributed in this situation .
The pack whose size is 30 is distributed among the group with
requirement 40 as 40 – 10 = 30 and 40 + 10 > 30.
The pack whose size is 60 is distributed among the group with 
requirement 60 as 60 – 10 < 60 and 60 + 10 > 60.
The pack whose size is 75 is distributed among the group with 
requirement 80 as 80 – 10 < 75 and 80 + 10 > 75.

Approach: The problem can be solved using sorting based on the following idea:

Sort both arrays. Now greedily find out the first group with a requirement that is fulfilled and among all the possible packs, allot the pack with minimum number of elements so that options are available for groups with higher requirements.

Follow the steps mentioned below to implement the idea:

  • Sort array A[] and B[].
  • After sorting both arrays use two pointers i and j to iterate A and B respectively.
  • Initialize a variable (say cnt = 0) to store the answer.
  • Now start iterating through the arrays:
    • If the pack size is desired for the group then increment cnt, i and j.
    • If the requirement of the group after subtracting the maximum difference K is greater than pack size then this pack is not useful for any group. Hence increment j.
    • If the pack size is greater than the requirement of the group after adding K, then there is no pack for that group. Hence increment i.
  • Return the cnt variable as the required answer.

Below is the implementation of the above approach.

C++

<?php
// Function performing the calculation
function desired_size($N, $M, $K, $A, $B)
{
    // Sorting vector A
    sort($A);
 
    // Sorting vector B
    sort($B);
 
    // Initialising pointer i, j
    // for A and B respectively
    $i = 0;
    $j = 0;
 
    // Initialising the variable to store answer
    $cnt = 0;
 
    // Loop to calculate the answer
    while ($i < $N && $j < $M) {
 
        // if the pack size is in between
        // the desired size then both the pointers
        // are incremented and count also incremented
        if ($A[$i] - $K <= $B[$j] && $A[$i] + $K >= $B[$j]) {
            $cnt++;
            $i++;
            $j++;
        }
 
        // If the pack size is smaller
        // than minimum group size after
        // subtracting the difference
        else if ($A[$i] - $K > $B[$j]) {
            $j++;
        }
 
        // If the pack size is larger
        // than minimum group size after
        // adding the difference
        else if ($A[$i] + $K < $B[$j]) {
            $i++;
        }
    }
 
    // Returning the count variable
    return $cnt;
}
 
// Driver code
$N = 4;
$M = 3;
$K = 5;
 
$A = array( 60, 45, 80, 60 );
$B = array( 30, 60, 75 );
 
// Function call
echo desired_size($N, $M, $K, $A, $B);
 
 // This code is contributed by Kanishka Gupta
?>

Output

2

Time Complexity: O(D * logD) where D is the maximum between N and M
Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Arrays

Related
Minimum money required to buy items with given cashbacks Minimum money required to buy items with given cashbacks
Maximize X such that Array can be sorted by swapping elements X distance apart Maximize X such that Array can be sorted by swapping elements X distance apart
Maximize the value of (A[i]-A[j])*A[k] for any ordered triplet of indices i, j and k Maximize the value of (A[i]-A[j])*A[k] for any ordered triplet of indices i, j and k
Find set of size K such that any value of the set is co-prime with any Array element Find set of size K such that any value of the set is co-prime with any Array element
Rearrange Array such that sum of adjacent sum divided by K is minimum Rearrange Array such that sum of adjacent sum divided by K is minimum

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13