Leap Year

Problem link : Leap Year

Problem: Check if a Year is a Leap Year

Problem Statement

You are given an integer n representing a year. Your task is to determine whether the year is a leap year or not.

Definition of a Leap Year:

A year is a leap year if:

Examples

Example 1:
Input: n = 4
Output: true
Explanation: 4 is divisible by 4 and not divisible by 100, so it's a leap year.

Example 2:
Input: n = 2021
Output: false
Explanation: 2021 is not divisible by 4, so it's not a leap year.


Intuition

To check whether a given year is a leap year, follow the leap year rules. These are well-known rules used in the Gregorian calendar.


Approach

Use a simple conditional check using the above rules:

(N % 4 == 0 && N % 100 != 0) || (N % 400 == 0)

This condition directly implements the leap year logic.


Code (Java)

// User function Template for Java
class Solution {
    static boolean isLeap(int N) {
        return (N % 4 == 0 && N % 100 != 0) || (N % 400 == 0);
    }
}

Time and Space Complexity


Dry Run

Input: N = 2000

Output: true

Input: N = 1900

Output: false

Input: N = 2024

Output: true


Conclusion

This problem is a direct application of well-established rules to determine leap years. It can be solved efficiently in constant time using basic conditional checks.