Maximize number of days for which P chocolates can be distributed consecutively to N people
Last Updated :
17 May, 2021
Given an integer, P denoting the number of chocolates and an array a[] where ai denotes the type of ith chocolate. There are N people who want to eat chocolate every day. Find the maximum number of consecutive days for which N people can eat chocolates considering the following conditions:
- Each of the N people must eat exactly one chocolate on a particular day.
- A single person can eat chocolate of the same type only for all days.
Examples:
Input: N = 4, P = 10, arr[] = {1, 5, 2, 1, 1, 1, 2, 5, 7, 2}
Output: 2
Explanation: Chocolates can be assigned in the following way:
Person 1: Type 1
Person 2: Type 1
Person 3: Type 2
Person 4: Type 5
In this way, there are sufficient chocolates for each person to eat one chocolate for two consecutive days. No other possible distribution of chocolates can make the people eat the chocolates for more than 2 days.
Input: N = 3, P = 10, arr[] = {1, 2, 2, 1, 1, 3, 3, 3, 2, 4}
Output: 3
Explanation: Chocolates can be distributed in the following way:
Person 1: Type 1
Person 2: Type 2
Person 3: Type 3
In this way, all the 3 people can eat their respective assigned type of chocolates for three days.
Approach: Follow the steps below to solve the problem:
- The minimum number of days for which it is possible to distribute the chocolates is 0 and the maximum number is P.
- So, for each number X in this range, check if it is possible to distribute chocolates to each person for X days.
- For all such Xs, find the maximum.
- Now, using Binary Search check for all the numbers in the range 0 to P.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
map< int , int > mp;
int N, P;
bool helper( int mid)
{
int cnt = 0;
for ( auto i : mp) {
int temp = i.second;
while (temp >= mid) {
temp -= mid;
cnt++;
}
}
return cnt >= N;
}
int findMaximumDays( int arr[])
{
for ( int i = 0; i < P; i++) {
mp[arr[i]]++;
}
int start = 0, end = P, ans = 0;
while (start <= end) {
int mid = start
+ ((end - start) / 2);
if (mid != 0 and helper(mid)) {
ans = mid;
start = mid + 1;
}
else if (mid == 0) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return ans;
}
int main()
{
N = 3, P = 10;
int arr[] = { 1, 2, 2, 1, 1,
3, 3, 3, 2, 4 };
cout << findMaximumDays(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
static int N, P;
static boolean helper( int mid)
{
int cnt = 0 ;
for (Map.Entry<Integer, Integer> i : mp.entrySet())
{
int temp = i.getValue();
while (temp >= mid)
{
temp -= mid;
cnt++;
}
}
return cnt >= N;
}
static int findMaximumDays( int arr[])
{
for ( int i = 0 ; i < P; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
int start = 0 , end = P, ans = 0 ;
while (start <= end)
{
int mid = start +
((end - start) / 2 );
if (mid != 0 && helper(mid))
{
ans = mid;
start = mid + 1 ;
}
else if (mid == 0 )
{
start = mid + 1 ;
}
else
{
end = mid - 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
N = 3 ;
P = 10 ;
int arr[] = { 1 , 2 , 2 , 1 , 1 ,
3 , 3 , 3 , 2 , 4 };
System.out.print(findMaximumDays(arr));
}
}
|
Python3
mp = {}
N, P = 0 , 0
def helper(mid):
cnt = 0 ;
for i in mp:
temp = mp[i]
while (temp > = mid):
temp - = mid
cnt + = 1
return cnt > = N
def findMaximumDays(arr):
for i in range (P):
mp[arr[i]] = mp.get(arr[i], 0 ) + 1
start = 0
end = P
ans = 0
while (start < = end):
mid = start + ((end - start) / / 2 )
if (mid ! = 0 and helper(mid)):
ans = mid
start = mid + 1
elif (mid = = 0 ):
start = mid + 1
else :
end = mid - 1
return ans
if __name__ = = '__main__' :
N = 3
P = 10
arr = [ 1 , 2 , 2 , 1 , 1 ,
3 , 3 , 3 , 2 , 4 ]
print (findMaximumDays(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
static int N, P;
static bool helper( int mid)
{
int cnt = 0;
foreach (KeyValuePair< int ,
int > i in mp)
{
int temp = i.Value;
while (temp >= mid)
{
temp -= mid;
cnt++;
}
}
return cnt >= N;
}
static int findMaximumDays( int []arr)
{
for ( int i = 0; i < P; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
int start = 0, end = P, ans = 0;
while (start <= end)
{
int mid = start +
((end - start) / 2);
if (mid != 0 && helper(mid))
{
ans = mid;
start = mid + 1;
}
else if (mid == 0)
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return ans;
}
public static void Main(String[] args)
{
N = 3;
P = 10;
int []arr = {1, 2, 2, 1, 1,
3, 3, 3, 2, 4};
Console.Write(findMaximumDays(arr));
}
}
|
Javascript
<script>
var mp = new Map();
var N, P;
function helper(mid)
{
var cnt = 0;
mp.forEach((value,) => {
var temp = value;
while (temp >= mid) {
temp -= mid;
cnt++;
}
});
return cnt >= N;
}
function findMaximumDays(arr)
{
for ( var i = 0; i < P; i++) {
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1);
}
var start = 0, end = P, ans = 0;
while (start <= end) {
var mid = start
+ parseInt((end - start) / 2);
if (mid != 0 && helper(mid)) {
ans = mid;
start = mid + 1;
}
else if (mid == 0) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return ans;
}
N = 3, P = 10;
var arr = [1, 2, 2, 1, 1,
3, 3, 3, 2, 4 ];
document.write( findMaximumDays(arr));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...