Advertisement
asdfg0998

Untitled

Nov 22nd, 2024
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. public String solution(String S, int K) {
  2.         // Days of the week in order
  3.         String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
  4.        
  5.         // Map days to their indices for quick lookup
  6.         Map<String, Integer> dayIndex = new HashMap<>();
  7.         for (int i = 0; i < days.length; i++) {
  8.             dayIndex.put(days[i], i);
  9.         }
  10.        
  11.         // Find the current day index
  12.         int currentIndex = dayIndex.get(S);
  13.        
  14.         // Calculate the new index using modular arithmetic
  15.         int newIndex = (currentIndex + K) % 7;
  16.        
  17.         // Return the corresponding day
  18.         return days[newIndex];
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement