Smallest number greater than X which is K | Function to find the smallest K periodic integer greater than X ; Stores the number in a temporary string ; Set X [ i ] = X [ i - k ] for i > k ; Start from the current index ; Loop upto N change X [ j ] to X [ i ] ; Return X if current Value is greater than original value ; Find the first digit not equal to 9 ; Increment X [ i ] ; Set POS to current index ; Change X [ i ] to 0 for all indices from POS + 1 to K ; Set X [ i ] = X [ i - k ] for i > k ; Loop upto N change X [ j ] to X [ i ] ; Return the final string ; Driver Code
def Kperiodicinteger ( X , N , K ) : NEW_LINE INDENT X = list ( X ) NEW_LINE temp = X . copy ( ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT X [ j ] = X [ i ] NEW_LINE j += K NEW_LINE DEDENT DEDENT if ( X >= temp ) : NEW_LINE INDENT return X NEW_LINE DEDENT POS = 0 NEW_LINE for i in range ( K - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( X [ i ] != '9' ) : NEW_LINE INDENT X [ i ] = str ( int ( X [ i ] ) + 1 ) NEW_LINE POS = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( POS + 1 , K ) : NEW_LINE INDENT X [ i ] = '0' NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT X [ j ] = X [ i ] NEW_LINE j += K NEW_LINE DEDENT DEDENT return X NEW_LINE DEDENT N = 4 NEW_LINE K = 2 NEW_LINE X = "1215" NEW_LINE print ( * Kperiodicinteger ( X , N , K ) , sep = ' ' ) NEW_LINE