Problem Solving and Algorithms
Algorithmic Problem Solving: From Requirements to Checked Steps
Algorithmic problem solving turns a stated need into precise steps you can check before writing a program. You use it when a task involves calculations, choices, or repeated actions. The central challenge is deciding exactly what the steps must accomplish for every allowed input, including unusual cases. A procedure that works for one convenient example may still fail to meet the requirements.
Specify the task
A problem specification states the allowed inputs, the required outputs, and the conditions connecting them. Write these down before choosing operations. Include units, permitted ranges, and any assumptions that affect the result.
For example, arranging transport requires more information than a student count. You need the capacity of each vehicle and whether the goal is to minimize vehicles, empty seats, or cost. Those goals can lead to different procedures. Resolve an ambiguous requirement rather than silently choosing an interpretation.
Separate a requirement from a proposed method. Saying that every student needs a seat describes the required result. Saying to divide by vehicle capacity proposes part of a method. This separation lets you judge a method against the task it is supposed to solve.
Build manageable steps
Decomposition means breaking a problem into smaller tasks whose results combine to solve the original problem. Each smaller task should have a clear purpose and produce information that another step can use.
Suppose you must place n objects into the fewest containers, each holding at most c objects. Assume that n is a nonnegative integer, c is a positive integer, and objects cannot be split. Find the number of full containers and the number of objects left over. The floor notation gives a convenient calculation:
Then choose the required number of containers, b:
Example: With 18 ribbons and space for 7 ribbons per envelope, there are 2 full envelopes and 4 ribbons left over. You therefore need 3 envelopes.
Express the steps in plain language or pseudocode. Identify which steps happen in sequence, which depend on a condition, and which repeat. If a step repeats, state what changes each time and when repetition ends.
Check the procedure against cases
Follow the steps manually and record changing values. Use the value produced by the latest completed step when a later step depends on it. This catches errors such as repeatedly using the original quantity instead of the remaining quantity.
Choose cases for a reason. For the container procedure, the following cases check different decisions:
Case | Required behaviour | Mistake it can expose |
|---|---|---|
No objects | Return zero containers | Adding a container unconditionally |
Exactly full containers | Add no extra container | Treating zero remainder as leftover objects |
Some objects left over | Add one container | Discarding the remainder |
Test values on both sides of a decision boundary, including equality. Also calculate the expected output independently of the proposed steps; otherwise, the same mistake can affect both your method and your check.
For the container procedure, the remainder satisfies 0≤r<c. A zero remainder needs no extra container; a positive remainder fits into exactly one more. This explains why the two cases cover every allowed input.
What to remember
- State inputs, outputs, units, and assumptions before choosing operations.
- Break the task into smaller tasks with useful results.
- Make choices, updates, and stopping conditions explicit.
- Check boundaries and explain why the procedure covers the allowed inputs.
Example: Plan buses without adding an unnecessary trip
A school hires identical buses with 28 passenger seats each. Every student needs a seat, and no other passengers travel. The school wants the fewest buses and the number of empty passenger seats. The student count n may be any nonnegative integer. A trace records the intermediate values produced as you follow a procedure for a particular input.
a) Formulate a procedure that takes n and returns both the required number of buses and the number of empty seats. Include the case of no students.
First, separate the task into counting full buses, checking for leftover students, and calculating unused seats. Add an extra bus only when students remain after filling the full buses.
- Read the nonnegative integer student count n.
- Calculate the full-bus count q=⌊n/28⌋.
- Calculate the remaining student count r=n−28q.
- If r=0, set the bus count b=q. Otherwise, set b=q+1.
- Calculate the empty-seat count e=28b−n.
- Return b buses and e empty seats.
When n=0, both q and r are zero. The procedure therefore returns zero buses and zero empty seats, as required.
The final procedure uses the remainder to decide whether another bus is needed, then subtracts occupied seats from the total seats provided.
b) Apply the procedure to 73 students. Find the number of buses and empty passenger seats.
First, calculate the number of full buses:
The full-bus count is ________. These buses seat 28×2=56 students.
Next, calculate the number of students left over:
There are ________ left after filling the first two buses. Because this count is positive, add one bus:
Finally, the buses provide ________, since 28×3=84. Subtract the occupied seats:
The school needs 3 buses with 11 empty passenger seats. Two buses would provide only 56 seats, so three is the smallest bus count that accommodates everyone.
c) Check the bus-count output for 0, 56, and 57 students. Explain what each test checks.
For zero students, calculate q=⌊0/28⌋=0 and r=0−28(0)=0. The zero-remainder branch returns b=0. This checks that the procedure does not hire a bus when nobody travels.
For 56 students, calculate q=⌊56/28⌋=2 and r=56−28(2)=0. The procedure returns b=2. This checks an exact multiple of capacity: no additional bus is needed.
For 57 students, calculate q=⌊57/28⌋=2 and r=57−28(2)=1. The positive-remainder branch returns b=2+1=3. This checks the first student beyond that exact-capacity boundary.
The expected outputs are 0 buses, 2 buses, and 3 buses, respectively. Together, these tests check the empty input case and both sides of the decision about adding another bus.
A charging station can allocate at most 21 kWh during a booking period. It processes requests of 8, 17, 6, and 5 kWh in that order. Start the allocated energy at zero. For each request, add it only if the resulting total would be at most 21 kWh; otherwise skip that request and continue. What is the final allocated energy, in kWh, to zero decimal places?
A game assigns a badge to each integer score from 0 through 100. Apply these rules in order: if the score is below 40, assign bronze; otherwise, if it is below 75, assign silver; otherwise assign gold. Complete the expected badge for every test score using the words bronze, silver, or gold.
| Score | Expected badge |
| 0 | |
| 39 | |
| 40 | |
| 74 | |
| 75 | |
| 100 |