Skip to content

Added a new code for finding largest date and time from given digits. #5568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.thealgorithms.backtracking;

import java.util.*;

public class LargestValidDateTimefromDigits {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String parts[] = input.split(",");
List<Integer> digits = new ArrayList<>();
for (String part : parts) {
digits.add(Integer.parseInt(part));
}
Collections.sort(digits, Collections.reverseOrder());
String result = findLargestValidDateTime(digits);
System.out.println(result);
}

// Function for latest valid Date Time
public static String findLargestValidDateTime(List<Integer> digits) {
List<Integer> month = findValidMonth(digits);
if (month == null)
return "0"; // No valid month

List<Integer> day = findValidDay(digits, month);
if (day == null)
return "0"; // No valid day

List<Integer> hour = findValidHour(digits, month, day);
if (hour == null)
return "0"; // No valid hour

List<Integer> minute = findValidMinute(digits, month, day, hour);
if (minute == null)
return "0"; // No valid minute

// Format the result: MM/DD HH:MM
return String.format("%02d/%02d %02d:%02d",
month.get(0) * 10 + month.get(1),
day.get(0) * 10 + day.get(1),
hour.get(0) * 10 + hour.get(1),
minute.get(0) * 10 + minute.get(1));
}

// Function for Valid Month
public static List<Integer> findValidMonth(List<Integer> digits) {
for (int i = 0; i < digits.size(); i++) {
for (int j = 0; j < digits.size(); j++) {
if (i != j) {
int month = digits.get(i) * 10 + digits.get(j);
if (month >= 1 && month <= 12) {
return Arrays.asList(digits.remove(i), digits.remove(j > i ? j - 1 : j));
}
}
}
}
return null; // No valid month found
}

// Function for Valid Day
public static List<Integer> findValidDay(List<Integer> digits, List<Integer> month) {
int maxDay = getMaxDays(month.get(0) * 10 + month.get(1)); // Get the max days of the selected month
for (int i = 0; i < digits.size(); i++) {
for (int j = 0; j < digits.size(); j++) {
if (i != j) {
int day = digits.get(i) * 10 + digits.get(j);
if (day >= 1 && day <= maxDay) {
return Arrays.asList(digits.remove(i), digits.remove(j > i ? j - 1 : j));
}
}
}
}
return null; // No valid day found
}

// Function for Valid Hour
public static List<Integer> findValidHour(List<Integer> digits, List<Integer> month, List<Integer> day) {
for (int i = 0; i < digits.size(); i++) {
for (int j = 0; j < digits.size(); j++) {
if (i != j) {
int hour = digits.get(i) * 10 + digits.get(j);
if (hour >= 0 && hour <= 23) {
return Arrays.asList(digits.remove(i), digits.remove(j > i ? j - 1 : j));
}
}
}
}
return null; // No valid hour found
}

// Function for Valid Minute
public static List<Integer> findValidMinute(List<Integer> digits, List<Integer> month, List<Integer> day,
List<Integer> hour) {
for (int i = 0; i < digits.size(); i++) {
for (int j = 0; j < digits.size(); j++) {
if (i != j) {
int minute = digits.get(i) * 10 + digits.get(j);
if (minute >= 0 && minute <= 59) {
return Arrays.asList(digits.get(i), digits.get(j));
}
}
}
}
return null; // No valid minute found
}

// Function for maximum days in a month
public static int getMaxDays(int month) {
// Max days in each month (index 0 = January)
int[] maxDaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return maxDaysInMonth[month - 1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.thealgorithms.backtracking;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

public class LargestValidDateTimefromDigitsTest {
@Test
public void testLargestValidDateTime() {
// Test case 1: All digits can form a valid date and time
List<Integer> digits1 = Arrays.asList(2, 0, 2, 1, 1, 2, 3, 4); // Example: 12/31 23:42
String expected1 = "12/31 23:42";
String result1 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits1);
assertEquals(expected1, result1);

// Test case 2: No valid date can be formed (invalid month)
List<Integer> digits2 = Arrays.asList(4, 5, 6, 7, 8, 9); // No valid month can be formed
String expected2 = "0"; // No valid datetime
String result2 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits2);
assertEquals(expected2, result2);

// Test case 3: Valid month but invalid day
List<Integer> digits3 = Arrays.asList(1, 2, 3, 4, 7, 9); // Valid month, but day > 31
String expected3 = "0"; // No valid datetime
String result3 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits3);
assertEquals(expected3, result3);

// Test case 4: Valid datetime with unused digits
List<Integer> digits4 = Arrays.asList(2, 0, 2, 1, 1, 9, 5, 6); // Example: 12/19 21:50
String expected4 = "12/19 21:50";
String result4 = LargestValidDateTimefromDigits.findLargestValidDateTime(digits4);
assertEquals(expected4, result4);
}
}
Loading