Largest Time for Given Digits - Problem

Given an array arr of exactly 4 digits, your task is to find the latest possible 24-hour time that can be formed using each digit exactly once.

A valid 24-hour time follows the format "HH:MM" where:

  • HH represents hours: from 00 to 23
  • MM represents minutes: from 00 to 59

Your goal is to arrange the four digits to create the maximum valid time. For example, if you have digits [1, 2, 3, 4], you want to find the arrangement that gives you the time closest to 23:59.

Return: The latest valid time as a string in "HH:MM" format, or an empty string "" if no valid time can be constructed.

Input & Output

example_1.py โ€” Basic Example
$ Input: arr = [1,2,3,4]
โ€บ Output: "23:41"
๐Ÿ’ก Note: The largest valid time we can make is 23:41. We use 2,3 for hours (23 โ‰ค 23) and 4,1 for minutes (41 โ‰ค 59). Other arrangements like 43:21 would be invalid since 43 > 23.
example_2.py โ€” No Valid Time
$ Input: arr = [5,5,5,5]
โ€บ Output: ""
๐Ÿ’ก Note: No valid time can be formed. Any arrangement would give 55:55, but the maximum valid hour is 23, so 55 hours is invalid.
example_3.py โ€” Edge Case with Zeros
$ Input: arr = [0,0,0,0]
โ€บ Output: "00:00"
๐Ÿ’ก Note: All zeros form the time 00:00, which is valid (midnight). Since there's only one possible arrangement, this is our answer.

Visualization

Tap to expand
Largest Time Formation Process2 3:4 1Target: Latest Valid Time (23:41)Available Digits:1234Testing Arrangements:12:34 โœ“ Valid21:43 โœ“ Valid23:41 โœ“ Valid (Maximum!)43:21 โœ— Invalid (43>23)34:12 โœ— Invalid (34>23)... (24 total combinations)๐Ÿ’ก With only 24 permutations, brute force checking is optimal!
Understanding the Visualization
1
Collect the digits
We have 4 digit tiles that need to be arranged in HH:MM format
2
Try all arrangements
Test each of the 24 possible ways to arrange these 4 digits
3
Validate each time
Check if hours โ‰ค 23 and minutes โ‰ค 59 for each arrangement
4
Find maximum
Among all valid times, select the one closest to 23:59
5
Return result
Return the latest valid time or empty string if none exist
Key Takeaway
๐ŸŽฏ Key Insight: Since we have exactly 4 digits with only 24 possible arrangements, generating and testing all permutations is the most straightforward and efficient approach - sometimes brute force IS the optimal solution!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Fixed 24 permutations regardless of input, so constant time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing a few variables and the result string

n
2n
โœ“ Linear Space

Constraints

  • arr.length == 4
  • 0 โ‰ค arr[i] โ‰ค 9
  • Each digit must be used exactly once
  • Time format must be HH:MM where HH โˆˆ [00,23] and MM โˆˆ [00,59]
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
24.7K 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