Distribution of candies according to ages of students
Given two integer arrays ages and packs where ages store the ages of different students and an element of pack stores the number of candies that packet has (complete array represent the number of packets). The candies can be distributed among students such that:
- Every student must get only one pack of candies.
- All students of the same age must get equal number of candies.
- A student which is older must get more candies than all the student who are younger than him.
The task is to determine whether it is possible to distribute candies in the described manner. If possible then print Yes else print No.
Examples:
Input: ages[] = {5, 15, 10}, packs[] = {2, 2, 2, 3, 3, 4}
Output: YES
There are 3 students with age 5, 15 and 10.And there are 6 packets of candies containing 2, 2, 2, 3, 3, 4 candies respectively.
We will give one packet containing 2 candies to the student of age 5, one packet containing 3 candies to student with age 10 and give the packet containing 4 candies to student age 15
Input: ages[] = {5, 5, 6, 7}, packs[] = {5, 4, 6, 6}
Output: NO
Approach:
- Make 2 frequency arrays, one which will store the number of students with a particular age and one which will store the number of packets with a particular amount of candies.
- Then traverse the frequency array for ages starting from the youngest age and for every age in ascending try to find the candy packets that are greater than or equal to the number of students for the selected age (starting from the least number of candies a packet has)
- If the above case fails then the answer is No else repeat the above steps until all the student have got the candies and print Yes in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void check_distribution( int n, int k,
int age[], int candy[])
{
int mxage = *(std::max_element(
age, age + n)) + 1;
int mxcandy = *(std::max_element(
candy, candy + k)) + 1;
int fr1[mxage] = {0};
int fr2[mxcandy] = {0};
for ( int j = 0; j < n; j++)
{
fr1[age[j]] += 1;
}
for ( int j = 0; j < k; j++)
{
fr2[candy[j]] += 1;
}
k = 0;
bool Tf = true ;
for ( int j = 0; j < mxage; j++)
{
if (fr1[j] == 0)
continue ;
bool flag = false ;
while (k < mxcandy)
{
if (fr1[j] <= fr2[k])
{
flag = true ;
break ;
}
k += 1;
}
k = k + 1;
if (flag == false )
{
Tf = false ;
break ;
}
}
if (Tf)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
int main()
{
int age[] = { 5, 15, 10 };
int candy[] = { 2, 2, 2, 3, 3, 4 };
int n = sizeof (age) / sizeof (age[0]);
int k = sizeof (candy) / sizeof (candy[0]);
check_distribution(n, k, age, candy);
return 0;
}
|
Java
import java.util.*;
class Main
{
public static void check_distribution( int n, int k,
int age[], int candy[])
{
int mxage = age[ 0 ];
for ( int i = 0 ; i < age.length; i++)
{
if (mxage < age[i])
{
mxage = age[i];
}
}
int mxcandy = candy[ 0 ];
for ( int i = 0 ; i < candy.length; i++)
{
if (mxcandy < candy[i])
{
mxcandy = candy[i];
}
}
int fr1[] = new int [mxage + 1 ];
Arrays.fill(fr1, 0 );
int fr2[] = new int [mxcandy + 1 ];
Arrays.fill(fr2, 0 );
for ( int j = 0 ; j < n; j++)
{
fr1[age[j]] += 1 ;
}
for ( int j = 0 ; j < k; j++)
{
fr2[candy[j]] += 1 ;
}
k = 0 ;
boolean Tf = true ;
for ( int j = 0 ; j < mxage; j++)
{
if (fr1[j] == 0 )
continue ;
boolean flag = false ;
while (k < mxcandy)
{
if (fr1[j] <= fr2[k])
{
flag = true ;
break ;
}
k += 1 ;
}
k = k + 1 ;
if (flag == false )
{
Tf = false ;
break ;
}
}
if (Tf)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
public static void main(String[] args)
{
int age[] = { 5 , 15 , 10 };
int candy[] = { 2 , 2 , 2 , 3 , 3 , 4 };
int n = age.length;
int k = candy.length;
check_distribution(n, k, age, candy);
}
}
|
Python3
def check_distribution(n, k, age, candy):
mxage = max (age) + 1
mxcandy = max (candy) + 1
fr1 = [ 0 ] * mxage
fr2 = [ 0 ] * mxcandy
for j in range (n):
fr1[age[j]] + = 1
for j in range (k):
fr2[candy[j]] + = 1
k = 0
Tf = True
for j in range (mxage):
if (fr1[j] = = 0 ):
continue
flag = False
while (k < mxcandy):
if (fr1[j] < = fr2[k]):
flag = True
break
k + = 1
k = k + 1
if (flag = = False ):
Tf = False
break
if (Tf):
print ( "YES" )
else :
print ( "NO" )
age = [ 5 , 15 , 10 ]
candy = [ 2 , 2 , 2 , 3 , 3 , 4 ]
n = len (age)
k = len (candy)
check_distribution(n, k, age, candy)
|
C#
using System.IO;
using System;
class GFG
{
static void check_distribution( int n, int k,
int [] age, int [] candy)
{
int mxage = age[0];
for ( int i = 0; i < age.Length; i++)
{
if (mxage < age[i])
{
mxage = age[i];
}
}
int mxcandy = candy[0];
for ( int i = 0; i < candy.Length; i++)
{
if (mxcandy < candy[i])
{
mxcandy = candy[i];
}
}
int [] fr1 = new int [mxage + 1];
Array.Fill(fr1, 0);
int [] fr2 = new int [mxcandy + 1];
Array.Fill(fr2, 0);
for ( int j = 0; j < n; j++)
{
fr1[age[j]] += 1;
}
for ( int j = 0; j < k; j++)
{
fr2[candy[j]] += 1;
}
k = 0;
bool Tf = true ;
for ( int j = 0; j < mxage; j++)
{
if (fr1[j] == 0)
{
continue ;
}
bool flag = false ;
while (k < mxcandy)
{
if (fr1[j] <= fr2[k])
{
flag = true ;
break ;
}
k += 1;
}
k = k + 1;
if (flag == false )
{
Tf = false ;
break ;
}
}
if (Tf)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
static void Main()
{
int [] age = {5, 15, 10};
int [] candy = { 2, 2, 2, 3, 3, 4 };
int n = age.Length;
int k = candy.Length;
check_distribution(n, k, age, candy);
}
}
|
Javascript
<script>
function check_distribution(n, k, age, candy)
{
let mxage = age[0];
for (let i = 0; i < age.length; i++)
{
if (mxage < age[i])
{
mxage = age[i];
}
}
let mxcandy = candy[0];
for (let i = 0; i < candy.length; i++)
{
if (mxcandy < candy[i])
{
mxcandy = candy[i];
}
}
let fr1 = new Array(mxage + 1);
fr1.fill(0);
let fr2 = new Array(mxcandy + 1);
fr2.fill(0);
for (let j = 0; j < n; j++)
{
fr1[age[j]] += 1;
}
for (let j = 0; j < k; j++)
{
fr2[candy[j]] += 1;
}
k = 0;
let Tf = true ;
for (let j = 0; j < mxage; j++)
{
if (fr1[j] == 0)
{
continue ;
}
let flag = false ;
while (k < mxcandy)
{
if (fr1[j] <= fr2[k])
{
flag = true ;
break ;
}
k += 1;
}
k = k + 1;
if (flag == false )
{
Tf = false ;
break ;
}
}
if (Tf)
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
}
let age = [5, 15, 10];
let candy = [ 2, 2, 2, 3, 3, 4 ];
let n = age.length;
let k = candy.length;
check_distribution(n, k, age, candy);
</script>
|
Time Complexity: O(maximum(n, k, max(age)))
Space Complexity: O(max(age))
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...