Candies - CSU2315 - Shoolini U

Candies

Candies

1. Problem Statement & Logic

Problem: Chef wants to give 1 candy to each of $N$ children. He currently has $X$ candies. He can buy packets containing 4 candies each. Find the minimum number of packets he needs to buy.

Logic:

1.1 Dry Run

Example: $N=20, X=12$

Example: $N=10, X=5$

1.2 Pseudocode Solution


READ T
FOR i FROM 1 TO T:
    READ N, X
    
    IF X >= N:
        PRINT 0
    ELSE:
        needed = N - X
        # Ceiling division (needed / 4)
        packets = (needed + 3) / 4 (integer division)
        PRINT packets
If X >= N, what is the output?

A: 0, because Chef already has enough candies ($X$) to give one to every child ($N$). No packets needed.