Start learning free

LESSON 75 OF 347 · CONDITIONAL STATEMENTS

Allow to Contest in C#: Range Check with &&

Practice Read the full explanation

At a contest, children can participate only if they have ages between minAge and maxAge. Given the age of a child, check if they are allowed or not.

Example 1:

Input: minAge = 7, maxAge = 12, age =  7
Output: true

Example 2:

Input: minAge = 3, maxAge = 8, age = 10
Output: false

Hints:

You can solve it in 3 different ways:
1. By checking for invalidations and return false when age < minAge and when age > maxAge, otherwise return true

2. Using one if statement with 2 conditions and return true, otherwise false

3. Since the function return value is a boolean, instead of using an if, you just return the condition since its value is a boolean.
STEP 1 OF 1
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS

    Allow To Contest in C#, explained

    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.

    minAgemaxAgeageResult
    7127true
    3810false

    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:

    1. Reject first. Return false if age < minAge or age > maxAge, otherwise return true.
    2. Accept with one if. Use one condition with two parts joined by &&, returning true inside the block and false after it.
    3. 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

    1. Lower limit. Express "not too young" as minAge <= age. Use <= because a child aged exactly minAge is allowed.
    2. Upper limit. Express "not too old" as age <= maxAge, with the same reasoning for the upper end.
    3. Combine. Join the two with &&, because both must be true.
    4. Return the result. The combined expression is a bool, so return it directly instead of wrapping it in an if.

    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 <= maxAge as a single chain in C#. minAge <= age produces a bool, and a bool cannot be compared with an int, 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 bool and the parameters are int values, in the order minAge, 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 = minAge or age = maxAge the answer is true. Using < in place of <= breaks the first example.
    • Using || instead of &&. minAge <= age || age <= maxAge is true for every age whenever minAge is not greater than maxAge, since any number is either at least the minimum or at most the maximum. The range needs &&.
    • Swapped limits. If minAge is greater than maxAge, no age can satisfy both comparisons, so the result is always false. 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 gives false.

    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) returns true.
    • AllowToContest(3, 8, 10) returns false.
    • AllowToContest(3, 8, 8) returns true, the upper boundary.
    • AllowToContest(3, 8, 2) returns false, one below the lower boundary.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free