|
| 1 | +package com.thealgorithms.backtracking; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class LargestValidDateTimefromDigits { |
| 6 | + public static void main(String args[]) { |
| 7 | + Scanner sc = new Scanner(System.in); |
| 8 | + String input = sc.nextLine(); |
| 9 | + String parts[] = input.split(","); |
| 10 | + List<Integer> digits = new ArrayList<>(); |
| 11 | + for (String part : parts) { |
| 12 | + digits.add(Integer.parseInt(part)); |
| 13 | + } |
| 14 | + Collections.sort(digits, Collections.reverseOrder()); |
| 15 | + String result = findLargestValidDateTime(digits); |
| 16 | + System.out.println(result); |
| 17 | + } |
| 18 | + |
| 19 | + // Function for latest valid Date Time |
| 20 | + public static String findLargestValidDateTime(List<Integer> digits) { |
| 21 | + List<Integer> month = findValidMonth(digits); |
| 22 | + if (month == null) |
| 23 | + return "0"; // No valid month |
| 24 | + |
| 25 | + List<Integer> day = findValidDay(digits, month); |
| 26 | + if (day == null) |
| 27 | + return "0"; // No valid day |
| 28 | + |
| 29 | + List<Integer> hour = findValidHour(digits, month, day); |
| 30 | + if (hour == null) |
| 31 | + return "0"; // No valid hour |
| 32 | + |
| 33 | + List<Integer> minute = findValidMinute(digits, month, day, hour); |
| 34 | + if (minute == null) |
| 35 | + return "0"; // No valid minute |
| 36 | + |
| 37 | + // Format the result: MM/DD HH:MM |
| 38 | + return String.format("%02d/%02d %02d:%02d", |
| 39 | + month.get(0) * 10 + month.get(1), |
| 40 | + day.get(0) * 10 + day.get(1), |
| 41 | + hour.get(0) * 10 + hour.get(1), |
| 42 | + minute.get(0) * 10 + minute.get(1)); |
| 43 | + } |
| 44 | + |
| 45 | + // Function for Valid Month |
| 46 | + public static List<Integer> findValidMonth(List<Integer> digits) { |
| 47 | + for (int i = 0; i < digits.size(); i++) { |
| 48 | + for (int j = 0; j < digits.size(); j++) { |
| 49 | + if (i != j) { |
| 50 | + int month = digits.get(i) * 10 + digits.get(j); |
| 51 | + if (month >= 1 && month <= 12) { |
| 52 | + return Arrays.asList(digits.remove(i), digits.remove(j > i ? j - 1 : j)); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return null; // No valid month found |
| 58 | + } |
| 59 | + |
| 60 | + // Function for Valid Day |
| 61 | + public static List<Integer> findValidDay(List<Integer> digits, List<Integer> month) { |
| 62 | + int maxDay = getMaxDays(month.get(0) * 10 + month.get(1)); // Get the max days of the selected month |
| 63 | + for (int i = 0; i < digits.size(); i++) { |
| 64 | + for (int j = 0; j < digits.size(); j++) { |
| 65 | + if (i != j) { |
| 66 | + int day = digits.get(i) * 10 + digits.get(j); |
| 67 | + if (day >= 1 && day <= maxDay) { |
| 68 | + return Arrays.asList(digits.remove(i), digits.remove(j > i ? j - 1 : j)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return null; // No valid day found |
| 74 | + } |
| 75 | + |
| 76 | + // Function for Valid Hour |
| 77 | + public static List<Integer> findValidHour(List<Integer> digits, List<Integer> month, List<Integer> day) { |
| 78 | + for (int i = 0; i < digits.size(); i++) { |
| 79 | + for (int j = 0; j < digits.size(); j++) { |
| 80 | + if (i != j) { |
| 81 | + int hour = digits.get(i) * 10 + digits.get(j); |
| 82 | + if (hour >= 0 && hour <= 23) { |
| 83 | + return Arrays.asList(digits.remove(i), digits.remove(j > i ? j - 1 : j)); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return null; // No valid hour found |
| 89 | + } |
| 90 | + |
| 91 | + // Function for Valid Minute |
| 92 | + public static List<Integer> findValidMinute(List<Integer> digits, List<Integer> month, List<Integer> day, |
| 93 | + List<Integer> hour) { |
| 94 | + for (int i = 0; i < digits.size(); i++) { |
| 95 | + for (int j = 0; j < digits.size(); j++) { |
| 96 | + if (i != j) { |
| 97 | + int minute = digits.get(i) * 10 + digits.get(j); |
| 98 | + if (minute >= 0 && minute <= 59) { |
| 99 | + return Arrays.asList(digits.get(i), digits.get(j)); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return null; // No valid minute found |
| 105 | + } |
| 106 | + |
| 107 | + // Function for maximum days in a month |
| 108 | + public static int getMaxDays(int month) { |
| 109 | + // Max days in each month (index 0 = January) |
| 110 | + int[] maxDaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 111 | + return maxDaysInMonth[month - 1]; |
| 112 | + } |
| 113 | +} |
0 commit comments