Open In App

Geeky Year

Improve
Improve
Like Article
Like
Save
Share
Report

It is given to you that on 1st January 2001 it was Monday. Let’s call a year as Geeky if the 1st January of that year happens to be on Sunday. There will be given two years ‘a‘ and ‘b‘. The task is to find the no. of Geeky years between those two years (including ‘a’ and ‘b’ as well)

Examples:

Input: a = 2001, b = 2013
Output: 2

Input: a = 2020, b = 2024
Output: 1

 

Approach: The idea is to store the days shift for each month and then calculate the answer. Follow the steps below to solve the problem:

  • Initialize the variable count as 0.
  • Iterate over the range [a, b] using the variable i and perform the following tasks:
    • Initialize the variable y as i-1.
    • Initialize the variable ans as (y + y / 4 – y / 100 + y / 400) % 7.
    • If ans equals 6 then increase the value of count by 1.
  • After performing the above steps, print the value of count as the answer.

Below is the implementation of the above approach.

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the total number
// of years
int Count(int a, int b)
{
 
    // Days shifts for each month
    int t[] = { 0, 3, 2, 5, 0, 3,
               5, 1, 4, 6, 2, 4 };
 
    // Store the answer
    int count = 0;
 
    // Traverse over the years
    for (int i = a; i <= b; i++) {
        int y = i - 1;
 
        int ans = (y + y / 4 - y / 100 + y / 400) % 7;
        if (ans == 6) {
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
 
    int a = 2001;
    int b = 2013;
    int ans = Count(a, b);
    cout << ans;
}
 
// This code is contributed by Samim Hossain Mondal.


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to count the total number
    // of years
    public static int Count(int a, int b)
    {
 
        // Days shifts for each month
        int t[] = { 0, 3, 2, 5, 0, 3,
                    5, 1, 4, 6, 2, 4 };
 
        // Store the answer
        int count = 0;
 
        // Traverse over the years
        for (int i = a; i <= b; i++) {
            int y = i - 1;
 
            int ans = (y + y / 4
                       - y / 100 + y / 400)
                      % 7;
            if (ans == 6) {
                count++;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int a = 2001;
        int b = 2013;
        int ans = Count(a, b);
        System.out.println(ans);
    }
}


Python3




# Python 3 program for the above approach
 
# Function to count the total number
# of years
def Count(a,  b):
 
    # Days shifts for each month
    t = [0, 3, 2, 5, 0, 3,
         5, 1, 4, 6, 2, 4]
 
    # Store the answer
    count = 0
 
    # Traverse over the years
    for i in range(a, b + 1):
        y = i - 1
 
        ans = (y + y // 4 - y // 100 + y // 400) % 7
        if (ans == 6):
            count += 1
 
    return count
 
# Driver Code
if __name__ == "__main__":
 
    a = 2001
    b = 2013
    ans = Count(a, b)
    print(ans)
 
    # This code is contributed by ukasp.


C#




// C# program for the above approach
using System;
 
class GFG {
 
  // Function to count the total number
  // of years
  public static int Count(int a, int b)
  {
 
    // Days shifts for each month
    int []t = { 0, 3, 2, 5, 0, 3,
               5, 1, 4, 6, 2, 4 };
 
    // Store the answer
    int count = 0;
 
    // Traverse over the years
    for (int i = a; i <= b; i++) {
      int y = i - 1;
 
      int ans = (y + y / 4
                 - y / 100 + y / 400)
        % 7;
      if (ans == 6) {
        count++;
      }
    }
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
 
    int a = 2001;
    int b = 2013;
    int ans = Count(a, b);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to count the total number
        // of years
        function Count(a, b) {
 
            // Days shifts for each month
            let t = [0, 3, 2, 5, 0, 3,
                5, 1, 4, 6, 2, 4];
 
            // Store the answer
            let count = 0;
 
            // Traverse over the years
            for (let i = a; i <= b; i++) {
                let y = i - 1;
 
                let ans = (y + Math.floor(y / 4)
                    - Math.floor(y / 100) + Math.floor(y / 400))
                    % 7;
                if (ans == 6) {
                    count++;
                }
            }
            return count;
        }
 
        // Driver Code
        let a = 2001;
        let b = 2013;
        let ans = Count(a, b);
        document.write(ans);
 
  // This code is contributed by Potta Lokesh
    </script>


 
 

Output

2

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Last Updated : 17 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads