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++
#include <bits/stdc++.h>
using namespace std;
int desired_size( int N, int M, int K,
vector< int >& A, vector< int >& B)
{
sort(A.begin(), A.end());
sort(B.begin(), B.end());
int i = 0, j = 0;
int cnt = 0;
while (i < N && j < M) {
if (A[i] - K <= B[j] && A[i] + K >= B[j]) {
cnt++;
i++;
j++;
}
else if (A[i] - K > B[j]) {
j++;
}
else if (A[i] + K < B[j]) {
i++;
}
}
return cnt;
}
int main()
{
int N = 4;
int M = 3;
int K = 5;
vector< int > A = { 60, 45, 80, 60 };
vector< int > B = { 30, 60, 75 };
cout << desired_size(N, M, K, A, B);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int desired_size( int N, int M, int K,
int [] A, int [] B)
{
Arrays.sort(A);
Arrays.sort(B);
int i = 0 , j = 0 ;
int cnt = 0 ;
while (i < N && j < M) {
if (A[i] - K <= B[j] && A[i] + K >= B[j]) {
cnt++;
i++;
j++;
}
else if (A[i] - K > B[j]) {
j++;
}
else if (A[i] + K < B[j]) {
i++;
}
}
return cnt;
}
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 };
System.out.print(desired_size(N, M, K, A, B));
}
}
|
Python3
def desired_size(N, M, K, A, B):
A.sort()
B.sort()
i, j = 0 , 0
cnt = 0
while (i < N and j < M):
if (A[i] - K < = B[j] and A[i] + K > = B[j]):
cnt + = 1
i + = 1
j + = 1
elif (A[i] - K > B[j]):
j + = 1
elif (A[i] + K < B[j]):
i + = 1
return cnt
if __name__ = = "__main__" :
N = 4
M = 3
K = 5
A = [ 60 , 45 , 80 , 60 ]
B = [ 30 , 60 , 75 ]
print (desired_size(N, M, K, A, B))
|
C#
using System;
public class GFG{
static int desired_size( int N, int M, int K,
int [] A, int [] B)
{
Array.Sort(A);
Array.Sort(B);
int i = 0, j = 0;
int cnt = 0;
while (i < N && j < M) {
if (A[i] - K <= B[j] && A[i] + K >= B[j]) {
cnt++;
i++;
j++;
}
else if (A[i] - K > B[j]) {
j++;
}
else if (A[i] + K < B[j]) {
i++;
}
}
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 };
Console.WriteLine(desired_size(N, M, K, A, B));
}
}
|
Javascript
<script>
function desired_size(N, M, K,
A, B) {
A.sort( function (a, b) { return a - b });
B.sort( function (a, b) { return a - b });
let i = 0, j = 0;
let cnt = 0;
while (i < N && j < M) {
if (A[i] - K <= B[j] && A[i] + K >= B[j]) {
cnt++;
i++;
j++;
}
else if (A[i] - K > B[j]) {
j++;
}
else if (A[i] + K < B[j]) {
i++;
}
}
return cnt;
}
let N = 4;
let M = 3;
let K = 5;
let A = [60, 45, 80, 60];
let B = [30, 60, 75];
document.write(desired_size(N, M, K, A, B));
</script>
|
PHP
<?php
function desired_size( $N , $M , $K , $A , $B )
{
sort( $A );
sort( $B );
$i = 0;
$j = 0;
$cnt = 0;
while ( $i < $N && $j < $M ) {
if ( $A [ $i ] - $K <= $B [ $j ] && $A [ $i ] + $K >= $B [ $j ]) {
$cnt ++;
$i ++;
$j ++;
}
else if ( $A [ $i ] - $K > $B [ $j ]) {
$j ++;
}
else if ( $A [ $i ] + $K < $B [ $j ]) {
$i ++;
}
}
return $cnt ;
}
$N = 4;
$M = 3;
$K = 5;
$A = array ( 60, 45, 80, 60 );
$B = array ( 30, 60, 75 );
echo desired_size( $N , $M , $K , $A , $B );
?>
|
Time Complexity: O(D * logD) where D is the maximum between N and M
Auxiliary Space: O(1)
Last Updated :
06 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...