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:

  • HH is between 00 and 23
  • MM is between 00 and 59

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
Largest Time for Given Digits: [1,2,3,4] → HH:MM1234Available DigitsPossible Times: 12:34, 12:43, 13:24, 13:42, 14:23, 14:32, 21:34, 21:43, 23:14, 23:41, ...Largest Valid Time23:41Hours: 23 ≤ 23 ✓ | Minutes: 41 ≤ 59 ✓Each digit used exactly once: 2,3,4,1
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
Asked in
Google 45 Facebook 32 Amazon 28
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen