Partition the digits of an integer such that it satisfies a given condition | Function to generate sequence from the given string ; Initialize vector to store sequence ; First add all the digits of group A from left to right ; Then add all the digits of group B from left to right ; Return the sequence ; Function that returns true if the sequence is non - decreasing ; Initialize result ; Function to partition the digits of an integer such that it satisfies the given conditions ; Convert the integer to string ; Length of the string ; Array to store the digits ; Storing the digits of X in array ; Initialize the result ; Loop through the digits ; Put into group A if digit less than D ; Put into group B if digit greater than D ; Put into group C if digit equal to D ; Loop through the digits to decide for group C digits ; Set flag equal to true if group B digit present ; If flag is true put in group A or else put in B ; Generate the sequence from partition ; Check if the sequence is non decreasing ; Return - 1 if no such partition is possible ; Driver code
def makeSeq ( s , a ) : NEW_LINE INDENT seq = [ ] ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' A ' ) : NEW_LINE INDENT seq . append ( a [ i ] ) ; NEW_LINE DEDENT DEDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' B ' ) : NEW_LINE INDENT seq . append ( a [ i ] ) ; NEW_LINE DEDENT DEDENT return seq ; NEW_LINE DEDENT def checkSeq ( v ) : NEW_LINE INDENT check = True ; NEW_LINE for i in range ( 1 , len ( v ) ) : NEW_LINE INDENT if ( v [ i ] < v [ i - 1 ] ) : NEW_LINE INDENT check = False ; NEW_LINE DEDENT DEDENT return check ; NEW_LINE DEDENT def digitPartition ( X ) : NEW_LINE INDENT num = str ( X ) ; NEW_LINE l = len ( num ) ; NEW_LINE a = [ 0 ] * l ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT a [ i ] = ( ord ( num [ i ] ) - ord ( '0' ) ) ; NEW_LINE DEDENT for D in range ( 10 ) : NEW_LINE INDENT res = " " ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( a [ i ] < D ) : NEW_LINE INDENT res += ' A ' ; NEW_LINE DEDENT elif ( a [ i ] > D ) : NEW_LINE INDENT res += ' B ' ; NEW_LINE DEDENT else : NEW_LINE INDENT res += ' C ' ; NEW_LINE DEDENT DEDENT flag = False ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( res [ i ] == ' B ' ) : NEW_LINE INDENT flag = True ; NEW_LINE DEDENT res = list ( res ) ; NEW_LINE if ( res [ i ] == ' C ' ) : NEW_LINE INDENT res [ i ] = ' A ' if flag else ' B ' ; NEW_LINE DEDENT DEDENT seq = makeSeq ( res , a ) ; NEW_LINE if ( checkSeq ( seq ) ) : NEW_LINE INDENT return " " . join ( res ) ; NEW_LINE DEDENT DEDENT return " - 1" ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 777147777 ; NEW_LINE print ( digitPartition ( X ) ) ; NEW_LINE DEDENT