Easy 0 solved 0% acceptance

Leap Year

Check whether a year follows the Gregorian leap-year rule.

What you'll learn

  • Combine divisibility rules with and/or
  • Apply the century exception
  • Write a clear one-line condition

Problem

A year divisible by 4 is a leap year unless it is also divisible by 100. Years divisible by 400 are leap years despite that exception. Apply this arithmetic rule to every allowed input year.

Implement leap_year(year).

Input

year: The year to check.

Output

Return true for a leap year and false otherwise.

Constraints

1 ≤ year ≤ 9999.

Examples

Example 1
Input:
year = 2024

Expected output:
expected = True
Input:
long long year = 2024;

Expected output:
bool expected = true;
Input:
long year = 2024;

Expected output:
bool expected = true;

Explanation

2024 is divisible by 4 and not by 100.

Example 2
Input:
year = 2100

Expected output:
expected = False
Input:
long long year = 2100;

Expected output:
bool expected = false;
Input:
long year = 2100;

Expected output:
bool expected = false;

Explanation

2100 is divisible by 100 but not by 400, so the century exception applies.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Most years follow the divisible-by-4 rule, but centuries are exceptions.

Hint 2

A year divisible by 400 is still a leap year, even though it is also divisible by 100.

Hint 3

Check the exception before rejecting century years, or you may misclassify years like 2000.

Expected complexity

O(1) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.