Maximum time in HH : MM : SS format that can be represented by given six digits | C ++ Program of the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestTimeFromDigits ( vector < int > & A ) { map < int , int > mp1 , mp2 ; for ( auto x : A ) { mp1 [ x ] ++ ; } mp2 = mp1 ; int hr = 23 , m = 59 , s = 59 ; while ( hr >= 0 ) { int h0 = hr / 10 , h1 = hr % 10 ; int m0 = m / 10 , m1 = m % 10 ; int s0 = s / 10 , s1 = s % 10 ; int p = 0 ; vector < int > arr { h0 , h1 , m0 , m1 , s0 , s1 } ; for ( auto & it : arr ) { if ( mp1 [ it ] > 0 ) { mp1 [ it ] -- ; } else { p = 1 ; } } if ( p == 0 ) { string s = " " ; s = to_string ( h0 ) + to_string ( h1 ) ; s += ' : ' + to_string ( m0 ) + to_string ( m1 ) ; s += ' : ' + to_string ( s0 ) + to_string ( s1 ) ; return s ; } mp1 = mp2 ; if ( s == 0 ) { s = 59 ; m -- ; } else if ( m < 0 ) { m = 59 ; hr -- ; } if ( s > 0 ) { s -- ; } } return " - 1" ; } int main ( ) { vector < int > v = { 0 , 2 , 1 , 9 , 3 , 2 } ; cout << largestTimeFromDigits ( v ) ; }