PROBLEM LINK:
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.