PROBLEM LINK:
Practice
Contest
Setter : Mohammad Salik
Tester : Hasan Jaddouh
Editorialist : Mohammad Salik
Difficulty
Cakewalk
Prerequisites
None
Problem
You are given an integer W and a string S where W is the number of days in the month and S is the first day of the month. You are required to find the frequencies of all the seven days(monday to sunday) in that month.
Explanation
Each day of the month will come at least 4 times in a month irrespective of the number of days in that month or the start day.
All 7 days coming 4 times each : 7*4=28
Number of Days left: W-28 (lets denote this by X )
Now we assign the frequency 5 to X days starting from S (The start day)
Rest of the days will be assigned the frequency 4
Alternate Explanation
We run a loop from 1 to W updating the count of days according to the day of start
for(int i=1;i<=W;i++)
{
count[id]++;
id++;
if(id==7)
{
id=0;
}
}
where id is initialized according to the first day .
id=0 represents S=mon
id=1 represents S=tues
and so on…
count[0] represent frequency of monday
count$[$1] represent frequency of tuesday
and so on…
Time Complexity
O(1) per testcase
Space Complexity
O(1)
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here .
Tester’s solution can be found here .
TO @jeioncs
i do not understand what real world implementation u are showing.
but look at calendar and see the month of november 2017
now add one more day i.e. 31st to november. Now calculate the number of occurance for each day.
And remember the starting day is monday here as written in problem statement.
the answer is correct.It doesn’t fail in the reality.
welcome to help.
adios.
jeioncs
December 31, 2017, 6:49am
3
To @ujjawal14
Perhaps i am missing something, what i want to say is…
[test data year 2017]
12
31 tues
28 tues
31 fri
30 sun
31 wed
30 fri
31 mon
31 thurs
30 sat
31 tues
30 thurs
30 sun
[Solution proposed in the challenge]
4 5 5 5 4 4 4
4 4 4 4 4 4 4
4 4 4 4 5 5 5
5 4 4 4 4 4 5
4 4 5 5 5 4 4
4 4 4 4 5 5 4
5 5 5 4 4 4 4
4 4 4 5 5 5 4
4 4 4 4 4 5 5
4 5 5 5 4 4 4
4 4 4 5 5 4 4
5 4 4 4 4 4 5
[Solution that i think is correct]
5 5 4 4 4 4 5
4 4 4 4 4 4 4
4 4 5 5 5 4 4
4 4 4 4 4 5 5
5 5 5 4 4 4 4
4 4 4 5 5 4 4
5 4 4 4 4 5 5
4 5 5 5 4 4 4
4 4 4 4 5 5 4
5 5 4 4 4 4 5
4 4 5 5 4 4 4
4 4 4 4 4 5 5
[Reality]
on a linux system --> ncal -M -b -y 2017 or
There is no need to invent days like November 31
I hope this is better explained.
Thanks for your quick answer.
Bye
I just see now!!!
I misinterpreted the problem. The day of the week in not the las day of the month, even the first.
Thanks for the patience an answers!!!
Thanks to @ujjawal14 and @paranoia
while (t-- > 0)
{
int W=s.nextInt();
String str=s.next();
int y=W%7;
if (str.equals(“mon”))
{
if (y==0) System.out.println(“4 4 4 4 4 4 4”);
if (y==1)System.out.println(“5 4 4 4 4 4 4”);
if (y==2)System.out.println(“5 5 4 4 4 4 4”);
if (y==3)System.out.println(“5 5 5 4 4 4 4”);
}
if (str.equals("tues"))
{
if (y==0) System.out.println("4 4 4 4 4 4 4");
if (y==1) System.out.println("4 5 4 4 4 4 4");
if (y==2) System.out.println("4 5 5 4 4 4 4");
if (y==3) System.out.println("4 5 5 5 4 4 4");
}
if (str.equals("wed"))
{
if (y==0) System.out.println("4 4 4 4 4 4 4");
if (y==1) System.out.println("4 4 5 4 4 4 4");
if (y==2) System.out.println("4 4 5 5 4 4 4");
if (y==3) System.out.println("4 4 5 5 5 4 4");
}
if (str.equals("thurs"))
{
if (y==0) System.out.println("4 4 4 4 4 4 4");
if (y==1) System.out.println("4 4 4 5 4 4 4");
if (y==2) System.out.println("4 4 4 5 5 4 4");
if (y==3) System.out.println("4 4 4 5 5 5 4");
}
if (str.equals("fri"))
{
if (y==0) System.out.println("4 4 4 4 4 4 4");
if (y==1) System.out.println("4 4 4 4 5 4 4");
if (y==2) System.out.println("4 4 4 4 5 5 4");
if (y==3) System.out.println("4 4 4 4 5 5 5");
}
if (str.equals("sat"))
{
if (y==0) System.out.println("4 4 4 4 4 4 4");
if (y==1) System.out.println("4 4 4 4 4 5 4");
if (y==2) System.out.println("4 4 4 4 4 5 5");
if (y==3) System.out.println("5 4 4 4 4 5 5");
}
if (str.equals("sun"))
{
if (y==0) System.out.println("4 4 4 4 4 4 4");
if (y==1) System.out.println("4 4 4 4 4 4 5");
if (y==2) System.out.println("5 4 4 4 4 4 5");
if (y==3) System.out.println("5 5 4 4 4 4 5");
}
}