Find the number of times every day occurs in a month
Last Updated :
16 Oct, 2022
Given the starting day and the number of days in a month. Find the number of times every day occurs in a month
Examples:
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:
CPP
#include <bits/stdc++.h>
using namespace std;
void occurrenceDays( int n, string firstday)
{
string days[] = { "Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" , "Saturday" , "Sunday" };
int count[7];
for ( int i = 0; i < 7; i++)
count[i] = 4;
int pos;
for ( int i = 0; i < 7; i++) {
if (firstday == days[i]) {
pos = i;
break ;
}
}
int inc = n - 28;
for ( int i = pos; i < pos + inc; i++) {
if (i > 6)
count[i % 7] = 5;
else
count[i] = 5;
}
for ( int i = 0; i < 7; i++) {
cout << days[i] << " " << count[i] << endl;
}
}
int main()
{
int n = 31;
string firstday = "Tuesday" ;
occurrenceDays(n, firstday);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static void occurrenceDays( int n, String firstday)
{
String[] days = new String[]{ "Monday" ,
"Tuesday" , "Wednesday" ,
"Thursday" , "Friday" ,
"Saturday" , "Sunday" };
int [] count = new int [ 7 ];
for ( int i = 0 ; i < 7 ; i++)
count[i] = 4 ;
int pos = 0 ;
for ( int i = 0 ; i < 7 ; i++)
{
if (firstday == days[i])
{
pos = i;
break ;
}
}
int inc = n - 28 ;
for ( int i = pos; i < pos + inc; i++)
{
if (i > 6 )
count[i % 7 ] = 5 ;
else
count[i] = 5 ;
}
for ( int i = 0 ; i < 7 ; i++)
{
System.out.println(days[i] + " " + count[i]);
}
}
public static void main(String argc[]){
int n = 31 ;
String firstday = "Tuesday" ;
occurrenceDays(n, firstday);
}
}
|
Python3
import math
def occurrenceDays( n, firstday):
days = [ "Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" , "Saturday" ,
"Sunday" ]
count = [ 4 for i in range ( 0 , 7 )]
pos = - 1
for i in range ( 0 , 7 ):
if (firstday = = days[i]):
pos = i
break
inc = n - 28
for i in range ( pos, pos + inc):
if (i > 6 ):
count[i % 7 ] = 5
else :
count[i] = 5
for i in range ( 0 , 7 ):
print (days[i] , " " , count[i])
n = 31
firstday = "Tuesday"
occurrenceDays(n, firstday)
|
C#
using System;
public class GfG {
public static void occurrenceDays( int n,
string firstday)
{
String[] days = new String[]{ "Monday" ,
"Tuesday" , "Wednesday" ,
"Thursday" , "Friday" ,
"Saturday" , "Sunday" };
int [] count = new int [7];
for ( int i = 0; i < 7; i++)
count[i] = 4;
int pos = 0;
for ( int i = 0; i < 7; i++)
{
if (firstday == days[i])
{
pos = i;
break ;
}
}
int inc = n - 28;
for ( int i = pos; i < pos + inc; i++)
{
if (i > 6)
count[i % 7] = 5;
else
count[i] = 5;
}
for ( int i = 0; i < 7; i++)
{
Console.WriteLine(days[i] + " "
+ count[i]);
}
}
public static void Main()
{
int n = 31;
string firstday = "Tuesday" ;
occurrenceDays(n, firstday);
}
}
|
Javascript
<script>
function occurrenceDays(n, firstday)
{
let days = [ "Monday" ,
"Tuesday" , "Wednesday" ,
"Thursday" , "Friday" ,
"Saturday" , "Sunday" ];
let count = [];
for (let i = 0; i < 7; i++)
count[i] = 4;
let pos = 0;
for (let i = 0; i < 7; i++)
{
if (firstday == days[i])
{
pos = i;
break ;
}
}
let inc = n - 28;
for (let i = pos; i < pos + inc; i++)
{
if (i > 6)
count[i % 7] = 5;
else
count[i] = 5;
}
for (let i = 0; i < 7; i++)
{
document.write(days[i] + " " + count[i] + "<br/>" );
}
}
let n = 31;
let firstday = "Tuesday" ;
occurrenceDays(n, firstday);
</script>
|
Output
Monday 4
Tuesday 5
Wednesday 5
Thursday 5
Friday 4
Saturday 4
Sunday 4
Time complexity: O(1)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...