Maximum possible time that can be formed from four digits | Python3 implementation of the approach ; Function to return the updated frequency map for the array passed as argument ; Function that returns true if the passed digit is present in the map after decrementing it 's frequency by 1 ; If map contains the digit ; Decrement the frequency of the digit by 1 ; True here indicates that the digit was found in the map ; Digit not found ; Function to return the maximum possible time_value in 24 - Hours format ; First digit of hours can be from the range [ 0 , 2 ] ; If no valid digit found ; If first digit of hours was chosen as 2 then the second digit of hours can be from the range [ 0 , 3 ] ; Else it can be from the range [ 0 , 9 ] ; Hours and minutes separator ; First digit of minutes can be from the range [ 0 , 5 ] ; Second digit of minutes can be from the range [ 0 , 9 ] ; Return the maximum possible time_value ; Driver code
from collections import defaultdict NEW_LINE def getFrequencyMap ( arr , n ) : NEW_LINE INDENT hashMap = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hashMap [ arr [ i ] ] += 1 NEW_LINE DEDENT return hashMap NEW_LINE DEDENT def hasDigit ( hashMap , digit ) : NEW_LINE INDENT if hashMap [ digit ] > 0 : NEW_LINE INDENT hashMap [ digit ] -= 1 NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE DEDENT def getMaxtime_value ( arr , n ) : NEW_LINE INDENT hashMap = getFrequencyMap ( arr , n ) NEW_LINE flag = False NEW_LINE time_value = " " NEW_LINE for i in range ( 2 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT flag = False NEW_LINE if ( time_value [ 0 ] == '2' ) : NEW_LINE INDENT for i in range ( 3 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 9 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT time_value += " : " NEW_LINE flag = False NEW_LINE for i in range ( 5 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT flag = False NEW_LINE for i in range ( 9 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT return time_value NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getMaxtime_value ( arr , n ) ) NEW_LINE DEDENT