 GFG App
Open App Browser
Continue

# 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++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function performing the calculation
int desired_size(int N, int M, int K,
vector<int>& A, vector<int>& B)
{
// Sorting vector A
sort(A.begin(), A.end());

// Sorting vector B
sort(B.begin(), B.end());

// Initialising pointer i, j
// for A and B respectively
int i = 0, j = 0;

// Initialising the variable to store answer
int 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
else if (A[i] + K < B[j]) {
i++;
}
}

// Returning the count variable
return cnt;
}

// Driver code
int main()
{
int N = 4;
int M = 3;
int K = 5;

vector<int> A = { 60, 45, 80, 60 };
vector<int> B = { 30, 60, 75 };

// Function call
cout << desired_size(N, M, K, A, B);
return 0;
}```

## Java

```// Java code for the above approach
import java.util.*;
public class GFG {

// Function performing the calculation
static int desired_size(int N, int M, int K,
int[] A, int[] B)
{
// Sorting vector A
Arrays.sort(A);

// Sorting vector B
Arrays.sort(B);

// Initialising pointer i, j
// for A and B respectively
int i = 0, j = 0;

// Initialising the variable to store answer
int 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
else if (A[i] + K < B[j]) {
i++;
}
}

// Returning the count variable
return cnt;
}

// driver code
public static void main(String[] args)
{
int N = 4;
int M = 3;
int K = 5;

int[] A = { 60, 45, 80, 60 };
int[] B = { 30, 60, 75 };

// Function call
System.out.print(desired_size(N, M, K, A, B));
}
}```

## Python3

```# python3 code to implement the approach

# Function performing the calculation
def desired_size(N, M, K, A, B):

# Sorting vector A
A.sort()

# Sorting vector B
B.sort()

# Initialising pointer i, j
# for A and B respectively
i, j = 0, 0

# Initialising the variable to store answer
cnt = 0

# Loop to calculate the answer
while (i < N and 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] and A[i] + K >= B[j]):
cnt += 1
i += 1
j += 1

# If the pack size is smaller
# than minimum group size after
# subtracting the difference
elif (A[i] - K > B[j]):
j += 1

# If the pack size is larger
# than minimum group size after
elif (A[i] + K < B[j]):
i += 1

# Returning the count variable
return cnt

# Driver code
if __name__ == "__main__":

N = 4
M = 3
K = 5

A = [60, 45, 80, 60]
B = [30, 60, 75]

# Function call
print(desired_size(N, M, K, A, B))

# This code is contributed by rakeshsahni
```

## C#

```// C# implementation
using System;
public class GFG{

// Function performing the calculation
static int desired_size(int N, int M, int K,
int[] A, int [] B)
{

// Sorting vector A
Array.Sort(A);

// Sorting vector B
Array.Sort(B);

// Initialising pointer i, j
// for A and B respectively
int i = 0, j = 0;

// Initialising the variable to store answer
int 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
else if (A[i] + K < B[j]) {
i++;
}
}

// Returning the count variable
return cnt;
}
static public void Main ()
{
int N = 4;
int M = 3;
int K = 5;

int [] A = { 60, 45, 80, 60 };
int [] B = { 30, 60, 75 };

// Function call
Console.WriteLine(desired_size(N, M, K, A, B));

}
}

// This code is contributed by ksam24000```

## Javascript

``` <script>
// JavaScript code for the above approach

// Function performing the calculation
function desired_size(N, M, K,
A, B) {
// Sorting vector A
A.sort(function (a, b) { return a - b });

// Sorting vector B
B.sort(function (a, b) { return a - b });

// Initialising pointer i, j
// for A and B respectively
let i = 0, j = 0;

// Initialising the variable to store answer
let 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
else if (A[i] + K < B[j]) {
i++;
}
}

// Returning the count variable
return cnt;
}

// Driver code
let N = 4;
let M = 3;
let K = 5;

let A = [60, 45, 80, 60];
let B = [30, 60, 75];

// Function call
document.write(desired_size(N, M, K, A, B));

// This code is contributed by Potta Lokesh

</script>```

## PHP

```<?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
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)

My Personal Notes arrow_drop_up