Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFind the day of the week after K days from the given...

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

Content has been removed on Author’s request.

Using Naive Approach:

Approach:

One approach to find the day of the week after K days from a given day is to use a brute-force method where we add K days to the given day and then find the corresponding day of the week. 

Algorithm:

Input the day and K
Add K days to the given day
Find the corresponding day of the week using the modulus operator (%)
Return the day of the week

Python3




days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
day = "Monday"
K = 100
new_day = (days_of_week.index(day) + K) % 7
print(days_of_week[new_day])


Output

Wednesday

This approach requires O(K) time complexity 

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments