Largest Time for Given Digits - Problem
Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.
24-hour times are formatted as "HH:MM", where:
HHis between00and23MMis between00and59
The earliest 24-hour time is 00:00, and the latest is 23:59.
Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string.
Input & Output
Example 1 — Basic Case
$
Input:
arr = [1,2,3,4]
›
Output:
23:41
💡 Note:
The largest valid time is 23:41. We use digits 2,3 for hours (23 ≤ 23) and digits 4,1 for minutes (41 ≤ 59).
Example 2 — No Valid Time
$
Input:
arr = [5,5,5,5]
›
Output:
""
💡 Note:
No valid time can be formed. The smallest hour would be 55:55, but 55 > 23 for hours and 55 > 59 for minutes.
Example 3 — Edge Case with Zeros
$
Input:
arr = [0,0,0,2]
›
Output:
20:00
💡 Note:
The largest valid time is 20:00. We use 2,0 for hours (20 ≤ 23) and 0,0 for minutes (00 ≤ 59).
Constraints
- arr.length == 4
- 0 ≤ arr[i] ≤ 9
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of 4 digits: [1,2,3,4]
2
Process
Try all arrangements to form HH:MM format
3
Output
Return largest valid time: 23:41
Key Takeaway
🎯 Key Insight: With only 4 digits, we can check all 24 permutations to find the maximum valid time
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code