Given the starting day and the number of days in a month. Find the number of times every day occurs in a month
Input : Number of days in month = 28 First day = Wednesday Output : Monday = 4 Tuesday = 4 Wednesday = 4 Thursday = 4 Friday = 4 Saturday = 4 Sunday = 4 Explanation: In the month of February, every day occurs 4 times. Input : Number of days in month = 31 First day = Tuesday Output : Monday = 4 Tuesday = 5 Wednesday = 5 Thursday = 5 Friday = 4 Saturday = 4 Sunday = 4 Explanation: The month starts on Tuesday and ends on Thursday.
Observations: We have to make some key observations. First one will be if the month has 28 days then every day occurs 4 times. The second one will be if it has 29 days then the day on which the month starts will occur 5 times. The third one will be if the month has 30 days, then the day on which the month starts and the next day will occur 5 days. The last one is if the month has 31 days, then the day on which the month starts and the next 2 days will occur 5 days with the rest occurring 4 times each.
Approach: Create an count array with size 7 and having an initial value of 4 as the minimum number of occurrence will be 4. (number of days-28). Find the index of the firstday. Calculate the number of days whose occurrence will be 5. Then run a loop from pos to pos+(number of days-28) to mark the occurrence as 5. If pos+(number of days-28) exceeds 6, then use %7 to get to the indexes from the beginning.
Below is the C++ implementation of the above approach:
Monday 4 Tuesday 5 Wednesday 5 Thursday 5 Friday 4 Saturday 4 Sunday 4
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Minimum number of times A has to be repeated such that B is a substring of it
- XOR of numbers that appeared even number of times in given Range
- Print all the sum pairs which occur maximum number of times
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Find row number of a binary matrix having maximum number of 1s
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
- Difference between two given times
- Remove elements from the array which appear more than k times
- Remove elements that appear strictly less than k times
- Largest sub-string where all the characters appear at least K times
- Largest element in the array that is repeated exactly k times
- Find the number of zeroes
- Find the Missing Number
- Find the slope of the given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.