Wrong answer in Gregorian Calendar Practise Question

My code for gregorian calendar practise problem is giving me wrong answer after submission. Initially I took only 1 condition ( modulus of 4 ) for my leap year part, but I rectified my mistake and compiled it again. Even though I am getting correct output for all the inputs, it is giving me wrong answer.
The link of the problem is https://www.codechef.com/problems/FLOW015
Here is my code:

#include<stdio.h>
#include<math.h>

int main()
{
// MAIN FORMULA - (year code + month code + century code + Date number - Leap year code)%7
// Here as Date is 1 & Month is January hence DATE NUMBER is 1 and MONTH CODE is 0
// If leap year SUBSTRACT 1 ELSE DON'T
// Year code formula - (YY +(YY/4))%7 where in 1898 YY is 98
// Century code - 1800s - 2; 1900s - 0; 2000s - 6; 2100s - 4; 2200s - 2; 2300s - 0;
int number_of_years, year_code, month_code, yy, century_code, date_number, leap_year, year[1000], day, i, j;
month_code = 0;
date_number = 1;
scanf("%d",&number_of_years);
for(i = 0; i<number_of_years; i++)
{
    scanf("\n%d",&year[i]);
}
for(j = 0; j<number_of_years; j++)
{  yy = 0; century_code = 0; year_code = 0; leap_year = 0; day = 0;
   {
        if(year[j]>=1800 && year[j]<=1899)
        {century_code = 2; yy = year[j] - 1800; year_code = ((yy+(yy/4))%7);}
        else if(year[j]>=1900 && year[j]<=1999)
        {century_code = 0; yy = year[j] - 1900; year_code = ((yy+(yy/4))%7);}
        else if(year[j]>=2000 && year[j]<=2099)
        {century_code = 6; yy = year[j] - 2000; year_code = ((yy+(yy/4))%7);}
        else if(year[j]>=2100 && year[j]<=2199)
        {century_code = 4; yy = year[j] - 2100; year_code = ((yy+(yy/4))%7);}
        else if(year[j]>=2200 && year[j]<=2299)
        {century_code = 2; yy = year[j] - 2200; year_code = ((yy+(yy/4))%7);}
        else if(year[j]>=2300 && year[j]<=2399)
        {century_code = 0; yy = year[j] - 2300; year_code = ((yy+(yy/4))%7);}
        else if(year[j]>=2400 && year[j]<=2499)
        {century_code = 6; yy = year[j] - 2400; year_code = ((yy+(yy/4))%7);}
        else if(year[j]>=2500 && year[j]<=2599)
        {century_code = 4; yy = year[j] - 2500; year_code = ((yy+(yy/4))%7);}
   }
        if((year[j]%4 == 0) && (year[j]%100 != 0))
        { leap_year = 1;}
        else if(year[j]%400 == 0)
        { leap_year = 1;}

   day = ((year_code +  month_code + century_code + date_number - leap_year)%7);

    switch(day)
	 {  case 0: printf("\nsunday") ;
					break ;
        case 1: printf("\nmonday") ;
				  break ;
		case 2: printf("\ntuesday") ;
					break ;
		case 3: printf("\nwednesday") ;
					break ;
		case 4: printf("\nthursday") ;
					break ;
		case 5: printf("\nfriday") ;
					break ;
		case 6: printf("\nsaturday") ;
					break ;
	  }
}


 return 0;
}

Ouch! I remember this was the most frustrating beginner level problem, atleast for me.

You can use google to verify the dates/days and try test cases. Usually 10 consecutive years gave away the part which was at fault.

You can use a simple formula for number of days in between and then use that to find what day it will be. And that can be a 1-2 liner (refer to my code - https://www.codechef.com/viewsolution/13791094 )

If my code fails to help, ping me, I will get back to you :slight_smile:

Thanks for replying. I tried exactly what you told me to do. I entered 10 consecutive years and verified and the days are MATCHING, all of them.
Infact I tried random years from 1800 to 2050 and again all of them are matching. I have no clue as to why they are not accepting my answer.

It is some edgy case for sure. Your code gives correct output on all of my inputs.

Theres one thing you can do, which many coders do to debug such cases. Give T=600, write a program on local pc to generate all years from [1900,2500].

Then run your code on it v/s a correct code on it. To find which year you are printing wrong output, you can write another short code which accepts the strings as input and tells which year they differ. (Okay, too much write a program, but thats honestly a good way to improve. )

It annoys me that I have to do this. I really appreciate your help. I will find a solution somehow.

Well, I feel you bro. But its better than trying random test cases for hours, because your code passes almost all cases i put it on…

If you are unable to do it, ping me and I will help :slight_smile:

Hey @parikhparth,

If we interpret the question in terms of finding number of leap years and from 2001 than that would be easy, now there are two conditions for a year to be leap year,

  1. multiple of 4 but not 100
  2. multiple of 400

So that’s it, for leap year add 2 otherwise add 1 to the actual day and at last take mod accordingly.

You can also see my submission here

Hope this helps!

He will appreciate if you can provide a counter case. His code worked for almost all cases i tried…

not able to find the mistake ://

https://ideone.com/e.js/dIii05 I tried to check it with my code by making a program comparing answer for all 600 inputs, but for every output answer was same :confused:

I suspect there are test cases outside the promised range. After I got a similar mysterious failure I extended the validity of my calculation back to 1582 (the calendar was enacted then) and indefinitely far forward, which passed.