You are given an Integer n. Return true if It is a Leap Year otherwise return false. A leap year is a year that contains an additional day, February 29th, making it 366 days long instead of the usual 365 days. Leap years are necessary to keep our calendar in alignment with the Earth's revolutions around the Sun.
Note: A year is a leap year if "any one of " the following conditions are satisfied:
- The year is multiple of 400.
- The year is a multiple of 4 and not a multiple of 100.
Example:
Input: n = 4
Output: true
Explanation: 4 is not divisible by 100 and is divisible by 4 so its a leap yearInput: n = 2021
Output: false
Explanation: 2021 is not divisible by 100 and is also not divisible by 4 so its not a leap year
Expected Approach - O(1) Time and O(1) Space
The basic idea is to check each number by dividing from 4, then again divide with 100, if we don't get 0 reminder then it is a Leap Year otherwise it is not.
#include <iostream>
using namespace std;
bool checkYear(int n) {
// Check if n is divisible by 4
if (n % 4 == 0) {
// If it's divisible by 100, it should also be
// divisible by 400 to be a leap year
if (n % 100 == 0) {
return n % 400 == 0;
}
return true;
}
return false;
}
int main() {
int year = 2000;
if (checkYear(year)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
#include <stdio.h>
int checkYear(int n) {
// Check if n is divisible by 4
if (n % 4 == 0) {
// If it's divisible by 100, it should also be
// divisible by 400 to be a leap year
if (n % 100 == 0) {
return n % 400 == 0;
}
return 1;
}
return 0;
}
int main() {
int year = 2000;
if (checkYear(year)) {
printf("true\n");
} else {
printf("false\n");
}
return 0;
}
class GfG {
static boolean checkYear(int n) {
// Check if n is divisible by 4
if (n % 4 == 0) {
// If it's divisible by 100, it should also be
// divisible by 400 to be a leap year
if (n % 100 == 0) {
return n % 400 == 0;
}
return true;
}
return false;
}
public static void main(String[] args) {
int year = 2000;
if (checkYear(year)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
def checkYear(n):
# Check if n is divisible by 4
if n % 4 == 0:
# If it's divisible by 100, it should also be
# divisible by 400 to be a leap year
if n % 100 == 0:
return n % 400 == 0
return True
return False
if __name__ == "__main__":
year = 2000
if checkYear(year):
print("true")
else:
print("false")
using System;
class GfG {
static bool CheckYear(int n) {
// Check if n is divisible by 4
if (n % 4 == 0) {
// If it's divisible by 100, it should also be
// divisible by 400 to be a leap year
if (n % 100 == 0) {
return n % 400 == 0;
}
return true;
}
return false;
}
static void Main() {
int year = 2000;
if (CheckYear(year)) {
Console.WriteLine("true");
} else {
Console.WriteLine("false");
}
}
}
function checkYear(n) {
// Check if n is divisible by 4
if (n % 4 === 0) {
// If it's divisible by 100, it should also be
// divisible by 400 to be a leap year
if (n % 100 === 0) {
return n % 400 === 0;
}
return true;
}
return false;
}
// Driver Code
let year = 2000;
if (checkYear(year)) {
console.log("true");
} else {
console.log("false");
}
Output
true
Time Complexity : O(1)
Auxiliary Space: O(1)