Find the day of the week after K days from the given day

Given current day as day of the week and an integer K, the task is to find the day of the week after K days.

Examples:

Input: day = “Monday”, K = 3
Output: Thursday

Input: day = “Tuesday”, K = 101
Output: Friday

Approach:



  • Store all the days of the week (Mon to Sun) in a list.
  • Find the index of the current day from that list.
  • Print the value stored in

    ((K + index) % 7)th index

    in the array.

Below code is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.*;
  
public class GFG {
  
    public static String dayFind(
        String newDay, int k)
    {
  
        // Create an array with
        // days of the week
        List<String> days
            = new ArrayList<String>() {{
            add("Monday");
            add("Tuesday");
            add("Wednesday");
            add("Thursday");
            add("Friday");
            add("Saturday");
            add("Sunday");
        } };
  
        // Find index of current
        // day in days array
        int index
            = days.indexOf(newDay);
  
        // return final day
        return days
            .get((index + k) % 7);
    }
  
    // Driver function
    public static void main(String[] args)
    {
  
        String str = "Monday";
  
        System.out.println(
            dayFind(str, 3));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def dayFind(newDay, k):
  
    # Create an array with
    # days of the week
    days = [ "Monday",
             "Tuesday",
             "Wednesday",
             "Thursday",
             "Friday",
             "Saturday",
             "Sunday" ]
  
    # Find index of current
    # day in days array
    index = 0
    for i in range(len(days)):
        if days[i] == newDay:
            index = i
  
    # Return final day
    return days[(index + k) % 7]
  
# Driver code
if __name__ == '__main__':
  
    str = "Monday"
    print(dayFind(str, 3))
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

using System;
using System.Collections.Generic;
class GFG{
  
public static String dayFind(String newDay, 
                                     int k)
{
  
    // Create an array with
    // days of the week
    List<String> days = new List<String>();
      
    days.Add("Monday");
    days.Add("Tuesday");
    days.Add("Wednesday");
    days.Add("Thursday");
    days.Add("Friday");
    days.Add("Saturday");
    days.Add("Sunday");
  
    // Find index of current
    // day in days array
    int index = days.IndexOf(newDay);
  
    // return readonly day
    return days[(index + k) % 7];
}
  
// Driver Code
public static void Main(String[] args)
{
    String str = "Monday";
  
    Console.WriteLine(dayFind(str, 3));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

Thursday

Time Complexity: O(1)
Space Complexity: O(1)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



Improved By : mohit kumar 29, Rajput-Ji