One-sentence summary
A problem usually has more than one correct solution, and we choose the best one for the situation by comparing these solutions against criteria such as correctness, simplicity, speed and safety.
Why does it matter?
When the first idea for solving a problem pops into our head, we often want to say "great, done" and stop there. But the fact that a solution works does not mean it is the best solution. Imagine two different paths that reach the same result: one may be short and clear, the other long and messy. Both give the right answer, yet one of them is far easier to read and to fix.
In real projects, solutions are preferred not just because they work, but because they are also fast, safe and easy to understand. That is why we learn to add a second question next to "does it work?": "how else could this be solved, and which way is better?"
Different algorithms for the same problem
A problem does not have to have a single correct solution. Usually there are several different paths that lead to the same goal. We remember from earlier lessons that we call these paths an algorithm: a clear set of steps followed to achieve a goal.
Let's think of an example: we want to add up all the numbers from 1 to 100. We can do this in at least two ways.
Way 1: Adding one by one with a loop
We take the numbers one at a time and collect them in a total variable.
Start
total = 0
for each number from 1 to 100
total = total + number
display total
End
This way performs 100 additions. Because it moves step by step, it is easy to picture in our heads.
Way 2: Calculating at once with a formula
In mathematics there is a ready-made formula for the sum of numbers from 1 to n: n × (n + 1) / 2.
Start
n = 100
total = n * (n + 1) / 2
display total
End
This way does only one multiplication, one addition and one division. So it finishes in three operations.
Both ways give the same result: 5050. But one uses 100 steps and the other uses 3. The result can be the same while the number of steps is very different.
Comparison criteria
So how do we decide which of two solutions is better? We look at several criteria together.
Correctness
This is the most basic criterion: does the solution really produce the right result? A solution that is not correct is useless no matter how fast it is. That is why we check correctness first.
Simplicity and readability
Can someone else (or you, a few weeks later) read and understand the solution afterwards? A short, tidy solution is easier to fix and improve than a long, tangled one.
Speed and number of steps
How many steps does the solution need to reach the result? In the addition example above, the formula way finishes in three operations while the loop way does a hundred. For small numbers the difference is unimportant, but when the number grows into the millions this difference becomes huge.
Resources and safety
How much memory, battery or time does the solution use? Is an algorithm running on a robot safe, or does it drive the robot into an obstacle? This criterion becomes important when resources are limited and a physical system is involved.
Everyday example: Which side should the robot pass?
There is an obstacle in front of a robot, and it needs to get to the other side. It can go around the obstacle on the right or on the left. Both solutions get the robot to its target, so both are correct.
Passing on the right
If an obstacle is in front of me
turn right
move forward two squares
turn left
move forward two squares
turn left
Passing on the left
If an obstacle is in front of me
turn left
move forward two squares
turn right
move forward two squares
turn right
These two solutions use almost the same number of steps. So the choice is decided by other criteria: Is there a wall on the right? Is the floor on the left safer? Which side does the robot's sensor see better? When the step count is equal, safety and the surrounding conditions decide.
That is why the answer to "which one is better?" always depends on the criteria and the situation.
Let's see both ways in Python
Let's compare the two ways of the addition example in real code.
# Way 1: Adding with a loop
total = 0
for number in range(1, 101):
total = total + number
print("Loop result:", total)
# Way 2: Adding with a formula
n = 100
total = n * (n + 1) // 2
print("Formula result:", total)
Both print lines display the same value: 5050. Even though the logic of the code is different, the result is the same. Here the // sign gives the whole-number part of the division.
A "good enough" solution
We do not have to find the fastest, shortest and most elegant solution for every problem. Very often a good enough solution does the job.
For example, if you need to add the numbers from 1 to 100 for a homework, the loop way is perfectly acceptable; it is fine even if you do not know the three-step formula. But if a program adds millions of numbers thousands of times a second, then the formula way really starts to matter.
Saying "good enough" does not mean ignoring the criteria. It means looking at the situation and deciding which criterion truly matters. Sometimes readability is more valuable than speed; sometimes it is the other way around.
Mini practice
On a sheet of paper, write two different solutions for the problem "find the sum of the numbers from 1 to 20":
- Write pseudocode that adds them one by one with a loop.
- Write pseudocode that calculates with the formula (
n * (n + 1) / 2).
Then fill in this table:
| Criterion | Loop way | Formula way |
|---|---|---|
| Does it give the right result? | ||
| How many operations? | ||
| Is it easy to read? |
At the end, ask yourself: Which one is "good enough" for this problem? Why?
Common mistakes
Only checking that it works
The most common mistake is to say "it works, so we're done." A solution can work but still be very hard to read, unsafe or needlessly slow. Working is a required criterion, but it is not the only one.
Ignoring readability and safety
If one of two paths that lead a robot to its target sends it past a dangerous edge, choosing that path just because it has fewer steps is not right. In the same way, code that no one understands becomes very hard to fix later.
Early optimism
Trusting the very first solution that comes to mind and never considering other paths is also a mistake. Thinking of at least one alternative before saying "this is definitely the best" usually leads to a better choice.
Lesson summary
- A problem often has more than one correct solution.
- We compare solutions using the criteria of correctness, simplicity/readability, speed/number of steps and resources/safety.
- The sum from 1 to 100 can be found in 100 steps with a loop or in 3 steps with a formula; the result is the same.
- When the step count is equal, safety and the surrounding conditions can decide the choice.
- A "good enough" solution does not ignore the criteria; it means choosing the right priority for the situation.
Check questions
- Does the fact that a solution works mean it is the best solution?
- Name the four criteria we use when comparing solutions.
- When we find the sum from 1 to 100 with the loop and formula ways, are the results the same? What can you say about the number of steps?
- If a robot can pass an obstacle on the right or left in the same number of steps, what decides the choice?
- What does a "good enough solution" mean?
Answers
- No. Working only satisfies the correctness criterion. A solution may be weak in readability, speed or safety, so working alone does not show that it is the best.
- Correctness, simplicity/readability, speed/number of steps, and resources/safety.
- The results are the same (5050). But the loop way does about 100 operations while the formula way does only 3, so the number of steps is very different.
- When the step count is equal, safety and the surrounding conditions decide: which side has an obstacle or wall, which floor is safer, which side the sensor sees better, and so on.
- It is a solution that meets the criteria that matter for the situation and gets the job done. It does not have to be the fastest or shortest; it is chosen by looking at which criterion is important.
Source and verification note
For “Comparing Multiple Solutions”, verification focuses on whether the relationship between Different algorithms for the same problem and Way 2: Calculating at once with a formula remains consistent across examples. The algorithms in this lesson are checked by tracing sample inputs by hand and comparing them with expected outputs. Pseudocode is used to make the reasoning sequence visible without tying it to one programming language.
Next lesson
Algorithm Mini Project: Smart Bag Check