Allow to Contest - Time Complexity: O(1) - C++ Solution


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

Understanding the Problem

The core challenge of this problem is to determine if a given age falls within a specified range. This is a common problem in scenarios where eligibility criteria are based on age, such as contests, sports events, or age-restricted activities.

Potential pitfalls include misunderstanding the inclusive nature of the age range or incorrectly implementing the boundary checks.

Approach

To solve this problem, we need to check if the given age is within the inclusive range defined by minAge and maxAge. There are several ways to implement this:

Naive Solution

A naive approach would involve checking if the age is less than minAge or greater than maxAge and returning false in those cases. Otherwise, return true. This approach is straightforward but can be optimized.

Optimized Solutions

1. Using a single if statement with two conditions to check if the age is within the range.

2. Directly returning the boolean result of the condition minAge <= age && age <= maxAge.

Algorithm

Let's break down the optimized approach:

  1. Check if the age is greater than or equal to minAge.
  2. Check if the age is less than or equal to maxAge.
  3. Return the result of the combined condition.

Code Implementation

// Function to check if a child is allowed to participate in the contest
bool isAllowedToContest(int minAge, int maxAge, int age) {
    // Return the result of the condition directly
    return minAge <= age && age <= maxAge;
}

Complexity Analysis

The time complexity of this solution is O(1) because it involves a constant number of operations regardless of the input size. The space complexity is also O(1) as no additional space is required.

Edge Cases

Consider the following edge cases:

These cases are handled by the condition minAge <= age && age <= maxAge.

Testing

To test the solution comprehensively, consider the following test cases:

Test Case 1:
Input: minAge = 7, maxAge = 12, age = 7
Expected Output: true

Test Case 2:
Input: minAge = 3, maxAge = 8, age = 10
Expected Output: false

Test Case 3:
Input: minAge = 5, maxAge = 10, age = 5
Expected Output: true

Test Case 4:
Input: minAge = 5, maxAge = 10, age = 10
Expected Output: true

Test Case 5:
Input: minAge = 5, maxAge = 10, age = 4
Expected Output: false

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to determine if a child is allowed to participate in a contest based on their age. We explored different approaches, provided a detailed algorithm, and implemented an optimized solution in C++. Understanding and solving such problems is crucial for developing strong problem-solving skills.

Additional Resources

For further reading and practice, consider the following resources: