Change language

# Distribution of sweets by age of students

| |

Given two integer arrays and where stores the ages of different students and the stores the number of chocolates in the package (the full array represents the number of packages). Candy can be distributed to students in such a way that:

1. Each student should receive only one box of candy.
2. All students of the same age should receive the same amount of candy.
3. The older student should receive more sweets than all the younger students.

The challenge is to determine if candy can be dispensed in this manner. If possible, print more print ,

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

Fit :

• Make 2 frequency arrays, one of which will store the number of students with a specific age and the other — number of bags with a certain amount of sweets.
• Then browse the frequency array for ages starting at the youngest age, and for each age in ascending order, try to find bags of sweets that are greater than or equal to the number of students for the selected age (starting with smallest number of candies in a bag)
• If the above case fails then answer otherwise repeat the steps above until all students will not receive candy and will print finally.

Below is the implementation of the above approach:

 ` # Python3 implementation of the approach `   ` # Function for checking the correctness of distribution enia ` ` def ` ` check_distribution (n, k, age, candy): `   ` # Consider maximum age all students + 1 ` ` mxage ` ` = ` ` max ` ` (age) ` ` + ` ` 1 ` ` `  ` ` ` # Destroying max candy + 1 ` ` mxcandy ` ` = ` ` max ` ` (candy) ` ` + ` ` 1 ` ` fr1 ` ` = ` ` [` ` 0 ` `] ` ` * ` ` mxage ` ` fr2 ` ` = ` ` [` ` 0 ` `] ` ` * ` ` mxcandy `   ` ` ` # create frequency array ` ` # age of students ` ` for ` ` j ` ` in ` ` range ` ` (n): ` ` fr1 [age [j]] ` ` + ` = ` 1 `   ` # Frequency array creation ` ` ` ` # candy bags ` ` for ` ` j ` ` in ` ` range ` ` (k): ` ` fr2 [candy [j]] ` ` + ` ` = ` ` 1 `   ` # a pointer to tell if we have reached ` ` # end of frequency array of sweets ` `  k = 0 ``   # Flag to tell if distribution is possible Tf = True for j in range (mxage): if (fr1 [j] = = 0 ): continue      # A flag to tell if we can select some   # candy bags for students aged j flag = False while (k & lt; mxcandy):   # If there are more packages # than or equal to the number of students age no j, then we can select these # student packages if (fr1 [j] " = fr2 [k]):   flag = True   break k  + = 1    # Start search from k + 1 in next operation k = k + 1   # If we can’t select any packages # then the answer is NO if (flag = = False ): `` Tf = ` ` False ` ` break ` ` ` ` if ` ` ( Tf): ` ` print ` ` (` ` "YES" ` `) ` ` ` ` else ` `: ` ` print ` ` (` ` "NO" ` `) `   ` # Driver code ` ` age ` ` = ` ` [` ` 5 ` `, ` ` 15 ` `, ` ` 10 ` `] ` ` candy ` ` = ` ` [` ` 2 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 4 ` `] ` ` n ` ` = ` ` len ` ` (age) ` ` k ` ` = ` ` len ` ` ( candy) ` ` check_distribution (n, k, age, candy) `

Exit :

` YES `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically