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
using System ; using System . Collections . Generic ; class GFG { static String largestTimeFromDigits ( int [ ] A ) { Dictionary < int , int > mp1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > mp2 = new Dictionary < int , int > ( ) ; foreach ( int x in A ) { if ( mp1 . ContainsKey ( x ) ) mp1 [ x ] = mp1 [ x ] + 1 ; else mp1 . Add ( x , 1 ) ; } mp2 = new Dictionary < int , int > ( 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 ; int [ ] arr = { h0 , h1 , m0 , m1 , s0 , s1 } ; foreach ( int it in arr ) { if ( mp1 . ContainsKey ( it ) && mp1 [ it ] > 0 ) { mp1 [ it ] = mp1 [ it ] - 1 ; } else { p = 1 ; } } if ( p == 0 ) { String st = " " ; st = String . Join ( " " , h0 ) + String . Join ( " " , h1 ) ; st += ' : ' + String . Join ( " " , m0 ) + String . Join ( " " , m1 ) ; st += ' : ' + String . Join ( " " , s0 ) + String . Join ( " " , s1 ) ; return st ; } mp1 = new Dictionary < int , int > ( mp2 ) ; if ( s == 0 ) { s = 59 ; m -- ; } else if ( m < 0 ) { m = 59 ; hr -- ; } if ( s > 0 ) { s -- ; } } return " - 1" ; } public static void Main ( String [ ] args ) { int [ ] v = { 0 , 2 , 1 , 9 , 3 , 2 } ; Console . Write ( largestTimeFromDigits ( v ) ) ; } }