Start learning free

LESSON 84 OF 410 · CONDITIONAL STATEMENTS - CHALLENGES

Leap Year in Java with O(1) Time Complexity

Practice Read the full explanation

A leap year happens every four years, so it's a year that is perfectly divisible by four.

However, if the year is a multiple of 100 (1800, 1900, etc), the year must be divisible by 400 to be leap.

Write a function that determines if the year is a leap year or not.

Examples:

year = 2020 ➞ true

year = 2021 ➞ false

year = 1968 ➞ true

year = 1900 ➞ false

Video Solution:

STEP 1 OF 4
    Ctrl + Enter to check

    JAVA · CONDITIONAL STATEMENTS - CHALLENGES

    Leap Year in Java, explained

    Understanding the Problem

    The core challenge of this problem is to correctly identify leap years based on the given rules. Leap years are significant in calendar systems and are used to keep our calendar year synchronized with the astronomical year.

    Common applications include date calculations, scheduling systems, and any software that deals with dates and times.

    Potential pitfalls include misunderstanding the rules for leap years, especially the exception for years that are multiples of 100 but not 400.

    Approach

    To solve this problem, we need to check the divisibility of the given year by 4, 100, and 400. Here's a step-by-step approach:

    1. If the year is divisible by 400, it is a leap year.
    2. If the year is divisible by 100 but not by 400, it is not a leap year.
    3. If the year is divisible by 4 but not by 100, it is a leap year.
    4. If the year is not divisible by 4, it is not a leap year.

    This approach ensures that all the rules for determining leap years are correctly applied.

    Algorithm

    Here is a step-by-step breakdown of the algorithm:

    1. Check if the year is divisible by 400. If true, return true.
    2. Check if the year is divisible by 100. If true, return false.
    3. Check if the year is divisible by 4. If true, return true.
    4. If none of the above conditions are met, return false.

    Code Implementation

    public class LeapYear {
        // Function to determine if a year is a leap year
        public static boolean isLeapYear(int year) {
            // Check if the year is divisible by 400
            if (year % 400 == 0) {
                return true;
            }
            // Check if the year is divisible by 100
            if (year % 100 == 0) {
                return false;
            }
            // Check if the year is divisible by 4
            if (year % 4 == 0) {
                return true;
            }
            // If none of the above conditions are met, it is not a leap year
            return false;
        }
    
        // Main method to test the function
        public static void main(String[] args) {
            System.out.println(isLeapYear(2020)); // true
            System.out.println(isLeapYear(2021)); // false
            System.out.println(isLeapYear(1968)); // true
            System.out.println(isLeapYear(1900)); // false
        }
    }
    

    Complexity Analysis

    The time complexity of this solution is O(1) because the operations (modulus and comparison) are constant time operations. The space complexity is also O(1) as we are not using any additional data structures.

    Edge Cases

    Potential edge cases include:

    • Years that are exactly divisible by 400 (e.g., 2000, 2400).
    • Years that are exactly divisible by 100 but not by 400 (e.g., 1900, 2100).
    • Years that are exactly divisible by 4 but not by 100 (e.g., 1996, 2024).
    • Years that are not divisible by 4 (e.g., 2019, 2021).

    These edge cases are handled by the algorithm as it checks each condition in the correct order.

    Testing

    To test the solution comprehensively, we can use a variety of test cases:

    • Simple cases: 2020 (true), 2021 (false).
    • Boundary cases: 2000 (true), 1900 (false).
    • Random cases: 1968 (true), 2100 (false).

    We can use testing frameworks like JUnit to automate these tests.

    Thinking and Problem-Solving Tips

    When approaching such problems, it is important to:

    • Understand the rules and constraints thoroughly.
    • Break down the problem into smaller, manageable parts.
    • Consider edge cases and how to handle them.
    • Write clean, readable code with comments explaining the logic.

    Practicing similar problems and studying algorithms can help improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to determine if a year is a leap year using a simple and efficient algorithm. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is important for developing strong problem-solving skills in programming.

    We encourage readers to practice and explore further to enhance their understanding and skills.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free