Skip to content

Commit a763b14

Browse files
committed
Added a new code for finding largest date and time from given digits
1 parent be8df21 commit a763b14

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
public class LargestValidDateTimefromDigitsTest {
11+
@Test
12+
public void testLargestValidDateTime() {
13+
// Test case 1: All digits can form a valid date and time
14+
List<Integer> digits1 = Arrays.asList(2, 0, 2, 1, 1, 2, 3, 4); // Example: 12/31 23:42
15+
String expected1 = "12/31 23:42";
16+
String result1 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits1);
17+
assertEquals(expected1, result1);
18+
19+
// Test case 2: No valid date can be formed (invalid month)
20+
List<Integer> digits2 = Arrays.asList(4, 5, 6, 7, 8, 9); // No valid month can be formed
21+
String expected2 = "0"; // No valid datetime
22+
String result2 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits2);
23+
assertEquals(expected2, result2);
24+
25+
// Test case 3: Valid month but invalid day
26+
List<Integer> digits3 = Arrays.asList(1, 2, 3, 4, 7, 9); // Valid month, but day > 31
27+
String expected3 = "0"; // No valid datetime
28+
String result3 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits3);
29+
assertEquals(expected3, result3);
30+
31+
// Test case 4: Valid datetime with unused digits
32+
List<Integer> digits4 = Arrays.asList(2, 0, 2, 1, 1, 9, 5, 6); // Example: 12/19 21:50
33+
String expected4 = "12/19 21:50";
34+
String result4 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits4);
35+
assertEquals(expected4, result4);
36+
}
37+
}

0 commit comments

Comments
 (0)