949. Largest Time for Given DigitsMedium6961048Add to ListShareGiven 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, and 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. Example 1:Input: arr = [1,2,3,4]Output: "23:41"Explanation: 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". Of these times, "23:41" is the latest.Example 2:Input: arr = [5,5,5,5]Output: ""Explanation: There are no valid 24-hour times as "55:55" is not valid. Constraints:arr.length == 40 <= arr[i] <= 9Accepted85,586Submissions243,556Seen this question in a real interview before?YesNoCompaniesRelated TopicsSimilar Questions
Question
- Largest Time for Given DigitsMedium6961048Add to ListShareGiven 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, and 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. Example 1:Input: arr = [1,2,3,4]Output: "23:41"Explanation: 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". Of these times, "23:41" is the latest.Example 2:Input: arr = [5,5,5,5]Output: ""Explanation: There are no valid 24-hour times as "55:55" is not valid. Constraints:arr.length == 40 <= arr[i] <= 9Accepted85,586Submissions243,556Seen this question in a real interview before?YesNoCompaniesRelated TopicsSimilar Questions
Solution
This problem can be solved by generating all permutations of the given array and checking which one forms a valid time. The steps are as follows:
-
Generate all permutations of the array. This can be done using a recursive function that swaps each element with every other element.
-
For each permutation, form a string in the format "HH:MM" and check if it is a valid time. A time is valid if HH is between 00 and 23 and MM is between 00 and 59.
-
Keep track of the maximum valid time encountered. This can be done by comparing the current time string with the maximum time string lexicographically.
-
Finally, return the maximum valid time. If no valid time was found, return an empty string.
Here is a Python solution for the problem:
from itertools import permutations
def largestTimeFromDigits(arr):
max_time = -1
# Generate all permutations of the array
for h, i, j, k in permutations(arr):
# Hours
hour = h*10 + i
# Minutes
minute = j*10 + k
if hour < 24 and minute < 60:
# Form a time
time = hour * 60 + minute
max_time = max(max_time, time)
if max_time == -1:
return ""
else:
return "{:02d}:{:02d}".format(max_time // 60, max_time % 60)
This function works by generating all permutations of the array and checking each one to see if it forms a valid time. The maximum valid time is kept track of and returned at the end. If no valid time is found, an empty string is returned.
Similar Questions
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, and 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.
Test time left: 01:28:08smaller elements to the right of that respective elementGiven an List of integers, you need to return a new list where each element in the new list is the number of smaller elements to the right of that respective element in the original input list.For example:You are given the List of integer values [3, 4, 9, 6, 1] and the resultant list you return is [1, 1, 2, 1, 0].Explanation:There is 1 smaller element to the right of `3`There is 1 smaller element to the right of `4`There are 2 smaller elements to the right of `9`There is 1 smaller element to the right of `6`There are no i.e. 0 smaller elements to the right of `1`Constraint: Input list can contain positive as well as negative integer values.You Just have to complete the function get_smaller_right(arr) where arr is the list of integer elements passed, and function will return the resultant array as required.Instruction: To execute your custom test cases, please provide input as mentioned in the visible sample test cases.Sample Test CasesTest Case 1:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1The·resultant·sub·array·is:·[1,·1,·2,·1,·0]Test Case 2:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5The·resultant·sub·array·is:·[0,·0,·0,·0,·0]Test Case 3:Expected Output:Enter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1The·resultant·sub·array·is:·[4,·3,·2,·1,·0]Submit12345678910111213141516def·get_smaller_right(arr):¬————n=len(arr)¬————result·=·[0]*n¬————for·i·in·range(n):¬————————count·=·0¬————————for·j·in·range(i+1,n):¬————————————if·arr[j]<arr[i]:¬————————————————count·+=1¬————————————result[i]·=·count¬————————return·result¬————————————¬————————————¬list1·=·[int(i)·for·i·in·input("Enter·the·elements·of·the·list·separated·by·spaces:·").split("·")]¬resultArray·=·get_smaller_right(list1)¬print("The·resultant·sub·array·is:",resultArray)¬·¶Execution Results 1 out of 3 shown cases successful0 out of 4 hidden cases successfulShow only failed cases Test Case - 1 (Execution Time: 3 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1Enter·the·elements·of·the·list·separated·by·spaces:·3 4 9 6 1The·resultant·sub·array·is:·[1,·1,·2,·1,·0] The·resultant·sub·array·is:·[1,·0,·0,·0,·0] : indicates the mismatch in the expected output. Test Case - 2 (Execution Time: 4 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5Enter·the·elements·of·the·list·separated·by·spaces:·1 2 3 4 5The·resultant·sub·array·is:·[0,·0,·0,·0,·0]The·resultant·sub·array·is:·[0,·0,·0,·0,·0] Test Case - 3 (Execution Time: 5 ms) Expected Output User OutputEnter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1Enter·the·elements·of·the·list·separated·by·spaces:·5 4 3 2 1The·resultant·sub·array·is:·[4,·3,·2,·1,·0] The·resultant·sub·array·is:·[4,·0,·0,·0,·0] : indicates the mismatch in the expected output.
Test time left: 35:50Choose the correct option.When a number is divided by 387, the remainder obtained is 48. If the same number is divided by 43, then the remainder obtained will be–Options30535
A clock is set right at 5 a.m. The clock loses 16 minutes in 24 hours.What will be the exact time when the clock indicates 10 p.m. on 4th day?9 p.m.0.510:45 p.m.11 p.m.
Using the 24 hour clock, what is the last minute of the current day and how would you say it?23:59 TIME TWENTY TREE FIVER NINE11:59 TIME WUN WUN FIFE NINE23:59 TIME TOO TREE FIFE NINE11:59 TIME ELEVEN FIVER NINE
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.