Leap Year

Easy
0/40
Average time to solve is 15m
profile
Contributed by
9 upvotes
Asked in companies
SAP LabsCuemath

Problem statement

You are given a year in the form of an integer 'N', and your task is to check whether the given year is a leap year or not.

Note:

1) A year is said to be a leap year if it has 366 days. Normal years have 365 days.

2) The integer 'N' does not contain any leading zeros.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer T denoting the number of test cases.

The first and the only line of each test case contains an integer 'N', denoting the year.
Output Format:
For each test case, return “Yes” if the input year is a leap year otherwise “No”.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1000 <= N <= 9999

Time limit: 1 sec
Sample Input 1:
2
2000
2020
Sample Output 1:
Yes
Yes
Explanation for Sample Output 1:
In test case 1, The year 2000 had 366 days since there were 29 days in the month of February in the year 2000.

In test case 2, The year 2020 had 366 days since there were 29 days in the month of February in the year 2020.
Sample Input 2:
2
1000
1600
Sample Output 2:
No
Yes
Explanation for Sample Output 2:
In test case 1, The year 1000 had 355 days since there were 28 days in the month of February in the year 1000.

In test case 2, The year 1600 had 366 days since there were 29 days in the month of February in the year 1600.
Hint

Use basic maths to find out something common in the leap years (Apart from the thing that they all have 366 days).

Approaches (1)
Basic Maths
  • We can make the following observations regarding every leap year:
  • If the year is divisible by 400 then it is a leap year. For example, 2000 was a leap year and it is divisible by 400.
  • If the year is not divisible by 400 and 100 but is divisible by 4 then also it is a leap year.  For example, 2020 is a leap year as it is not divisible by 400 and 100 but is divisible 4.
  • Let's take another example: the year 1000 was not a leap year because it is not divisible by 400 but it is divisible by 100.
  • So, using the above observations we can check if the year is divisible by 400 or if it is not divisible by 100 but is divisible by 4 then we return true(i.e. the input year is a leap year) otherwise, we return false(i.e. the input year is not a leap year).
Time Complexity

O(1),

 

Since all operations are performed in constant time. Thus the time complexity will be O(1).

Space Complexity

O(1)

 

Since we are using constant extra memory. Thus the space complexity will be O(1).

Code Solution
(100% EXP penalty)
Leap Year
Full screen
Console