Day of the week | Practice | GeeksforGeeks

Write a program that calculates the day of the week for any particular date in the past or future.
Example 1:
Input:
d = 28, m = 12, y = 1995
Output:
Thursday
Explanation:
28 December 1995 was a Thursday.
Example 2:
Input:
d = 30, m = 8, y = 2010
Output:
Monday
Explanation:
30 August 2010 was a Monday.

Your Task:
You don't need to read input or print anything. Your task is to complete the function getDayOfWeek() which takes 3 Integers d, m and y denoting day, month and year as input and return a String denoting the answer.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= d <= 31
1 <= m <= 12
1990 <= y <= 2100
0
aloksinghmp552 weeks ago
in this question it says 29/02/2100 ,it will be monday and 
01/03/2100 ,it will also monday. how can it possible.
what is wrong in this code ???
string getDayOfWeek(int d, int m, int y) {      string day[7]={ "Sunday","Monday","Tuesday","Wednesday","Thursday",      "Friday","Saturday"};      int i,j;      int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};       long long sum=0;      for(int i=1990;i<y;i++){          if(i%4==0){              sum=sum+366;          }          else{              sum=sum+365;          }      }      for(j=1;j<m;j++){          if(j==2 && (y%4==0)){              sum=sum+1;          }          sum=sum+mon[j];      }      sum=sum+d;      return day[sum%7];   }
+1
rahulgupta067892 months ago
string getDayOfWeek(int d, int m, int y) {       string days[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};       static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };       y -= m < 3;       int n=( y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;       return days[n];   }
0
ankitparashxr4 months ago
lass Solution {   static String getDayOfWeek(int d, int m, int y)    {                String days[] ={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",};      //k is  the day of the month.     //m is the month number.     //D is the last two digits of the year.     //C is the first two digits of the year.         if(m<3)       {           m+=12;           y-=1;       }     int k = d;
     int D = y%100;     int C = y/100;     int x =(C/4-2*C+D+D/4+13*(m+1)/5+k-1)%7;     int val = x%7;     if(val<0)     {         int num = val/7;         num = num-1;         int num2 = num*7;         int num3 = val-num2;         return days[num3];     }     return days[x%7];   }};
0
bhavit mathur11 months ago
bhavit mathur
string getDayOfWeek(int d, int m, int y) {
        static int t[] = { 0, 3, 2, 5, 0, 3,                       5, 1, 4, 6, 2, 4 };    y -= m < 3;    int x= ( y + y / 4 - y / 100 +             y / 400 + t[m - 1] + d) % 7;
      if(x==0)      {          return "Sunday";      }      if(x==1)      {          return "Monday";      }      if(x==2)      {          return "Tuesday";      }      if(x==3)      {          return "Wednesday";      }      if(x==4)      {          return "Thursday";      }      if(x==5)      {          return "Friday";      }      if(x==6)      {          return "Saturday";      }
    }
0
Raj Kumar11 months ago
Raj Kumar
TC : 0.42Map<integer ,="" string=""> map = new HashMap<>();        map.put(0,"Sunday");        map.put(1,"Monday");        map.put(2,"Tuesday");        map.put(3,"Wednesday");        map.put(4,"Thursday");        map.put(5,"Friday");        map.put(6,"Saturday");
        //the Tomohiko Sakamoto Algorithm        int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
        // if month is less than 3 reduce year by 1        if (m < 3)            y -= 1;
        int day =  (y + (y/4) - (y/100) + (y/400) + (t[m - 1] + d)) % 7;
         String dayOftheWeek= map.get(day);
         return dayOftheWeek;
0
Bharat Gupta1 year ago
Bharat Gupta
Test cases don't match with constraints. Mine gave a WA for year 1435.
0
Aman Kumar Mishra1 year ago
Aman Kumar Mishra
The test cases used are wrong. For 1 1 492 , it is showing correct answer is Tuesday but actually it should be Wednesday.
Here is the correct code in java 1.8:-
static String getDayOfWeek(int d, int m, int y) {        // code here        Map<integer,integer> monthCode = new HashMap<integer,integer>();        monthCode.put(1,1);        monthCode.put(2,4);        monthCode.put(3,4);        monthCode.put(4,0);        monthCode.put(5,2);        monthCode.put(6,5);        monthCode.put(7,0);        monthCode.put(8,3);        monthCode.put(9,6);        monthCode.put(10,1);        monthCode.put(11,4);        monthCode.put(12,6);        Map<integer,string> dayCode = new HashMap<integer,string>();        dayCode.put(0,"Saturday");        dayCode.put(1,"Sunday");        dayCode.put(2,"Monday");        dayCode.put(3,"Tuesday");        dayCode.put(4,"Wednesday");        dayCode.put(5,"Thursday");        dayCode.put(6,"Friday");        int n = y;        int count = 0;        int sum = 0,r;        while(count < 2)        {            r = n % 10;            n = n / 10;            sum = sum + (int)Math.pow(10,count)*r;            count++;        }        int leap = sum / 4;        sum = d + monthCode.get(m) + centuryCode(n) + sum + sum/4;        int ans = sum % 7;        return dayCode.get(ans);    }    static int centuryCode(int century)    {        int[] code = {6,4,2,0};        int x = 0, res = 0;        for(int i = 0; i <= century; i++)        {            res = code[x%4];            x++;        }        return res;    }
0
SUMITHRA S2 years ago
SUMITHRA S
import datetimedef dateday(l):    week_days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']    day=datetime.date(l[2],l[1],l[0]).weekday()    print(week_days[day])for i in range(int(input())):    l=list(map(int,input().split()))    dateday(l)
0
Riya kashyap2 years ago
Riya kashyap
solution in javaimport java.util.*;import java.lang.*;import java.io.*;import java.time.LocalDate;class GFG {public static void main (String[] args) { //code Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t--!=0) {     int day = sc.nextInt();     int month = sc.nextInt();     int year = sc.nextInt();    LocalDate days = LocalDate.of(year,month,day);        String s = days.getDayOfWeek().toString();        char y[] = s.toCharArray();        for(int i=1;i<y.length;i++) {="" y[i]="(char)(y[i]+32);" }="" system.out.println(y);="" }="" }="" }="">
0
Shashwat Sharma2 years ago
Shashwat Sharma
0.01 sec...#include <iostream>using namespace std;
int main() {//codeint t; cin>>t;while(t-->0){    int day,month,year;   cin>>day>>month>>year;    if (month == 1)         {             month = 13;             year--;         }         if (month == 2)         {             month = 14;             year--;         }         int q = day;         int m = month;         int k = year % 100;         int j = year / 100;         int h = q + 13*(m + 1) / 5 + k + k / 4 + j / 4 + 5 * j;         h = h % 7;         switch (h)         {             case 0 : cout<<"Saturday"<<endl; break;="" case="" 1="" :="" cout<<"sunday"<<endl;="" break;="" case="" 2="" :="" cout<<"monday"<<endl;="" break;="" case="" 3="" :="" cout<<"tuesday"<<endl;="" break;="" case="" 4="" :="" cout<<"wednesday"<<endl;="" break;="" case="" 5="" :="" cout<<"thursday"<<endl;="" break;="" case="" 6="" :="" cout<<"friday"<<endl;="" break;="" }="" }="" return="" 0;="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.