Smallest number greater than X which is K | Java program to find the smallest K periodic integer greater than X ; 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
import java . util . * ; class GFG { static String Kperiodicinteger ( char [ ] X , int N , int K ) { String temp = String . valueOf ( X ) ; for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( String . valueOf ( X ) . compareTo ( temp ) >= 0 ) { return String . valueOf ( X ) ; } int POS = 0 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != '9' ) { X [ i ] ++ ; POS = i ; break ; } } for ( int i = POS + 1 ; i < K ; i ++ ) { X [ i ] = '0' ; } for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return String . valueOf ( X ) ; } public static void main ( String [ ] args ) { int N = 4 , K = 2 ; String X = "1215" ; System . out . print ( Kperiodicinteger ( X . toCharArray ( ) , N , K ) ) ; } }