


Given an array/list ‘ARR’ having 4 integer digits only. The task is to return the maximum 24 hour time that can be formed using the digits from the array.
Note:
The minimum time in 24-hour format is 00:00, and the maximum is 23:59. If a valid time cannot be formed then return -1.
Example:
We have an array ARR = {1, 2, 3, 4} so the maximum time that will be formed will be 23:41.
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains four integers.
Output format:
For each test case, return the maximum time if found otherwise return -1.
Output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just return the valid string.
1 <= T <= 10
0 <= ARR[i] <= 9
Time Limit: 1 sec
2
1 2 3 4
5 5 5 5
23:41
-1
For the first test case,
The valid 24-hour times are "12:34", "12:43", "13:24", "13:42",
"14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Out of all
these, "23:41" is the maximum.
For the second test case,
There are no valid 24-hour times as "55:55" is not valid.
2
0 0 0 0
0 1 0 0
00:00
10:00
Try to think by generating all possible permutations and find the maximum possible time.
Approach:
Algorithm:
O(1).
The total number of permutations will be 4! = 24. Since the length of the array is fixed, therefore to generate all the permutations will be done in constant time. Also, each iteration takes a constant time to process and since the total number of permutations is fixed therefore it will take 24*O(1) time. Thus total time complexity will be O(1) + O(1) = O(1) time.
O(1).
In the algorithm, we keep the permutations for the input digits, which are in total 24, i.e. a constant number regardless of the input.