Find maximum possible time HHMM by permuting four given digits
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computing and mathematics, finding the maximum possible time in the HH:MM format by permuting four given digits is an interesting problem that draws upon combinatorial logic and number theory. This problem can have numerous applications ranging from simple programming exercises to complex time representation logic in embedded systems.
Problem Definition
Given four digits, we want to determine the latest possible time that can be represented using these digits in a 24-hour format. The time must be valid, where the hour (`HH`) falls between `00` and `23`, and the minutes (`MM`) fall between `00` and `59`.
Approach to Solution
Step 1: Understanding Permutations
The first task is to generate all possible permutations of the four digits. Since these digits need to be used to form a valid time, each permutation will consider:
- The first two digits as the hours (`HH`).
- The last two digits as the minutes (`MM`).
Given four digits, there will be a total of permutations calculated by permutations. However, not all permutations will form valid times.
Step 2: Filtering Valid Times
Once you have all possible permutations, the challenge becomes filtering those that meet the criteria for valid times:
- The first permutation of digits must form a number between `00` and `23`.
- The second permutation must form a number between `00` and `59`.
Step 3: Finding the Maximum Time
After filtering valid times, the final step is to determine which of these represents the latest possible time. This is achieved by comparing these valid times and selecting the maximum.
There are several ways to implement this in a programming environment:
- Brute Force Method: Iteratively check each permutation, record the valid ones, and find the maximum.
- Efficient Check Method: By using bounds checking (ensuring hours are `<24` and minutes `<60`) during permutation generation, fewer checks are needed, making the algorithm faster.
Example
Consider the digits `1, 9, 2, 3`:
- Generate permutations: (1923, 1932, 1293, 1239, ..., 3912).
- Filter for valid times: Valid times include (19:23, 19:32, 13:29, 12:39, etc.).
- Determine the maximum valid time: Out of all valid options, 19:32 is the latest.
Implementation
Below is a simple Python function to demonstrate this approach:
- Identical Digits: When digits are all the same or similar (e.g., `1, 1, 1, 1`), the outcome may remain fixed, yielding only one valid time if at all.
- Inappropriate Mappings: Avoid assuming that permutation order will naturally yield valid times without bounds checking, as this is a frequent source of errors.
- No Valid Time: It’s possible that a set of digits may not form any valid time (e.g., `9, 9, 9, 9`), meaning appropriate handling in the code should return an "Invalid Time" notice.
Related reading
- Find minimal Ai2 Bi2 when A and B are sorted
- Find most efficient groups of pairs
- Find next highest unique number from the given digits
- Find number of permutations of a given sequence of integers which yield the same binary search tree
- Find out which combinations of numbers in a set add up to a given total
- Find Pythagorean triplet for which a b c 1000
- Find rank of a decimal number based on function F N rank
- Find rectangles that contain point – Efficient Algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.