Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count of Leap Years in a given year range

  • Difficulty Level : Basic
  • Last Updated : 09 Nov, 2021

Given two years L and R, the task is to find the total number of leap years possible in the range (L, R) inclusive.
Examples: 
 

Input: L = 1, R = 400 
Output: 97
Input: L = 400, R = 2000 
Output: 389 
 

 

Naive Approach: The idea is to iterate through L to R and check if the year is a leap year or not using this approach. 
Time complexity: O(N)
Efficient Approach: 
 

  1. A year is a leap year if the following conditions are satisfied: 
    • Year is multiple of 400.
    • Year is multiple of 4 and not multiple of 100.
  2. So calculate the numbers which satisfy above condition in range (1, L) and (1, R) by 
     

Number of Leap years in (1, year) = (year / 4) – (year / 100) + (year / 400) 
 

  1.  
  2. Difference of the number of Leap years in range (1, R) with the number of leap years in range (1, L) will be the desired output.

Below is the implementation of the above approach.
 

C++




// C++ implementation to find the 
// count of leap years in given
// range of the year 
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to calculate the number
// of leap years in range of (1, year)
int calNum(int year)
{
    return (year / 4) - (year / 100) +
                        (year / 400);
}
  
// Function to calculate the number
// of leap years in given range
void leapNum(int l, int r)
{
    l--;
    int num1 = calNum(r);
    int num2 = calNum(l);
    cout << num1 - num2 << endl;
}
  
// Driver Code
int main()
{
    int l1 = 1, r1 = 400;
    leapNum(l1, r1);
  
    int l2 = 400, r2 = 2000;
    leapNum(l2, r2);
  
    return 0;
}

Java




// Java implementation to find the 
// count of leap years in given
// range of the year 
class GFG
{
  
// Function to calculate the number
// of leap years in range of (1, year)
static int calNum(int year)
{
    return (year / 4) - (year / 100) +
                        (year / 400);
}
  
// Function to calculate the number
// of leap years in given range
static void leapNum(int l, int r)
{
    l--;
    int num1 = calNum(r);
    int num2 = calNum(l);
    System.out.print(num1 - num2 +"\n");
}
  
// Driver Code
public static void main(String[] args)
{
    int l1 = 1, r1 = 400;
    leapNum(l1, r1);
  
    int l2 = 400, r2 = 2000;
    leapNum(l2, r2);
}
}
  
// This code is contributed by PrinciRaj1992

Python3




# Python3 implementation to find the 
# count of leap years in given 
# range of the year 
  
# Function to calculate the number 
# of leap years in range of (1, year) 
def calNum(year) : 
  
    return (year // 4) - (year // 100) + (year // 400
  
# Function to calculate the number 
# of leap years in given range 
def leapNum(l, r) :
  
    l -= 1 
    num1 = calNum(r)
    num2 = calNum(l)
    print(num1 - num2)
  
# Driver Code 
if __name__ == "__main__"
  
    l1 = 1
    r1 = 400 
    leapNum(l1, r1)
  
    l2 = 400
    r2 = 2000 
    leapNum(l2, r2)
  
# This code is contributed by AnkitRai01

C#




// C# implementation to find the 
// count of leap years in given
// range of the year 
using System;
  
class GFG
{
  
// Function to calculate the number
// of leap years in range of (1, year)
static int calNum(int year)
{
    return (year / 4) - (year / 100) +
                        (year / 400);
}
  
// Function to calculate the number
// of leap years in given range
static void leapNum(int l, int r)
{
    l--;
    int num1 = calNum(r);
    int num2 = calNum(l);
    Console.Write(num1 - num2 +"\n");
}
  
// Driver Code
public static void Main(String[] args)
{
    int l1 = 1, r1 = 400;
    leapNum(l1, r1);
  
    int l2 = 400, r2 = 2000;
    leapNum(l2, r2);
}
}
  
// This code is contributed by PrinciRaj1992

Javascript




<script>
    // Javascript implementation to find the 
    // count of leap years in given
    // range of the year 
      
    // Function to calculate the number
    // of leap years in range of (1, year)
    function calNum(year)
    {
        return parseInt(year / 4, 10) - parseInt(year / 100, 10) + parseInt(year / 400, 10);
    }
  
    // Function to calculate the number
    // of leap years in given range
    function leapNum(l, r)
    {
        l--;
        let num1 = calNum(r);
        let num2 = calNum(l);
        document.write((num1 - num2) +"</br>");
    }
      
    let l1 = 1, r1 = 400;
    leapNum(l1, r1);
    
    let l2 = 400, r2 = 2000;
    leapNum(l2, r2);
  
// This code is contributed by divyeh072019.
</script>
Output: 
97
389

 

Time Complexity: O(1)
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!