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
function Kperiodicinteger ( X , N , K ) { var temp = X for ( var i = 0 ; i < K ; i ++ ) { var j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( parseInt ( X . join ( ' ' ) ) < parseInt ( temp . join ( ' ' ) ) ) { return X . join ( ' ' ) ; } var POS ; for ( var i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != ' ' ) { X [ i ] = String . fromCharCode ( X [ i ] . charCodeAt ( 0 ) + 1 ) ; POS = i ; break ; } } for ( var i = POS + 1 ; i < K ; i ++ ) { X [ i ] = ' ' ; } for ( var i = 0 ; i < K ; i ++ ) { var j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return X . join ( ' ' ) ; } var N = 4 , K = 2 ; var X = " " ; document . write ( Kperiodicinteger ( X . split ( ' ' ) , N , K ) ) ;