Open In App

Geeky Year

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:

Below is the implementation of the above approach.




// 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 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);
    }
}




# 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# 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.




<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)

 


Article Tags :