Maximum time in HH : MM : SS format that can be represented by given six digits | Function to return the maximum possible time in 24 - Hours format that can be represented by array elements ; Stores the frequency of the array elements ; Maximum possible time ; Iterate to minimum possible time ; Conditions to reduce the the time iteratively ; If all required digits are present in the Map ; Retrieve Original Count ; If seconds is reduced to 0 ; If minutes is reduced to 0 ; Driver Code
def largestTimeFromDigits ( A ) : NEW_LINE INDENT mp1 = { } NEW_LINE mp2 = { } NEW_LINE for x in A : NEW_LINE INDENT mp1 [ x ] = mp1 . get ( x , 0 ) + 1 NEW_LINE DEDENT mp2 = mp1 . copy ( ) NEW_LINE hr = 23 NEW_LINE m = 59 NEW_LINE s = 59 NEW_LINE while ( hr >= 0 ) : NEW_LINE INDENT h0 = hr // 10 NEW_LINE h1 = hr % 10 NEW_LINE m0 = m // 10 NEW_LINE m1 = m % 10 NEW_LINE s0 = s // 10 NEW_LINE s1 = s % 10 NEW_LINE p = 0 NEW_LINE arr = [ h0 , h1 , m0 , m1 , s0 , s1 ] NEW_LINE for it in arr : NEW_LINE INDENT if ( it in mp1 and mp1 . get ( it ) > 0 ) : NEW_LINE INDENT mp1 [ it ] = mp1 . get ( it ) - 1 NEW_LINE DEDENT else : NEW_LINE INDENT p = 1 NEW_LINE DEDENT DEDENT if ( p == 0 ) : NEW_LINE INDENT s = " " NEW_LINE s = ( str ( h0 ) + str ( h1 ) ) NEW_LINE s += ( ' : ' + str ( m0 ) + str ( m1 ) ) NEW_LINE s += ( ' : ' + str ( s0 ) + str ( s1 ) ) NEW_LINE return s NEW_LINE DEDENT mp1 = mp2 . copy ( ) NEW_LINE if ( s == 0 ) : NEW_LINE INDENT s = 59 NEW_LINE m -= 1 NEW_LINE DEDENT elif ( m < 0 ) : NEW_LINE INDENT m = 59 NEW_LINE hr -= 1 NEW_LINE DEDENT if ( s > 0 ) : NEW_LINE INDENT s -= 1 NEW_LINE DEDENT DEDENT return " - 1" NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT v = [ 0 , 2 , 1 , 9 , 3 , 2 ] NEW_LINE print ( largestTimeFromDigits ( v ) ) NEW_LINE DEDENT