Problem overview
A contest accepts children whose age lies between minAge and maxAge. Given a child's age, the method AllowToContest returns true if the child may take part, and false otherwise.
| minAge | maxAge | age | Result |
|---|---|---|---|
| 7 | 12 | 7 | true |
| 3 | 8 | 10 | false |
In the first row the age equals the lower limit and is still allowed, so both limits are inclusive. In the second, 10 is above the upper limit of 8.
Approach
There are several ways to write it, and they all express the same rule:
- Reject first. Return
falseifage < minAgeorage > maxAge, otherwise returntrue. - Accept with one
if. Use one condition with two parts joined by&&, returningtrueinside the block andfalseafter it. - Return the condition. The two-part test already evaluates to a
bool, so return it directly.
This lesson uses the third form, the shortest one. The rule is "age is at least minAge and at most maxAge". Two comparisons, both must hold, so && joins them.
Here is the first form, for comparison:
if (age < minAge || age > maxAge) {
return false;
}
return true;
It reads "reject anyone outside the range", and it is equivalent to the version we use. The two are mirror images: "outside the range" is age < minAge || age > maxAge, and "inside the range" is the opposite, which swaps || for && and flips each comparison. Writing the positive form saves the extra if.
Step by step
- Lower limit. Express "not too young" as
minAge <= age. Use<=because a child aged exactlyminAgeis allowed. - Upper limit. Express "not too old" as
age <= maxAge, with the same reasoning for the upper end. - Combine. Join the two with
&&, because both must be true. - Return the result. The combined expression is a
bool, soreturnit directly instead of wrapping it in anif.
The solution in C#
class Solution {
public bool AllowToContest(int minAge, int maxAge, int age) {
return minAge <= age && age <= maxAge;
}
}
Writing the comparisons in this order makes the code look like the mathematical range minAge <= age <= maxAge, with age in the middle.
C# details
- You cannot write
minAge <= age <= maxAgeas a single chain in C#.minAge <= ageproduces abool, and aboolcannot be compared with anint, so the compiler rejects it. Two comparisons joined by&&are required. - Comparison operators bind tighter than
&&, so no parentheses are needed around each side. - The return type is
booland the parameters areintvalues, in the orderminAge,maxAge,age. Mixing up that order is easy, because all three are integers and the compiler cannot tell.
Edge cases and common mistakes
- Inclusive boundaries. With
age = minAgeorage = maxAgethe answer istrue. Using<in place of<=breaks the first example. - Using
||instead of&&.minAge <= age || age <= maxAgeis true for every age wheneverminAgeis not greater thanmaxAge, since any number is either at least the minimum or at most the maximum. The range needs&&. - Swapped limits. If
minAgeis greater thanmaxAge, no age can satisfy both comparisons, so the result is alwaysfalse. The code does not need special handling for that. - Negative or zero ages. They follow the same rule. With
minAge = 3, an age of 0 givesfalse.
Complexity
The method does two comparisons and one logical operation, whatever the input values. Time is O(1) and extra space is O(1).
Testing
AllowToContest(7, 12, 7)returnstrue.AllowToContest(3, 8, 10)returnsfalse.AllowToContest(3, 8, 8)returnstrue, the upper boundary.AllowToContest(3, 8, 2)returnsfalse, one below the lower boundary.