# Smallest number greater than X which is K-periodic

Given a string integer X consisting of N digits and an integer K, the task is to find the smallest integer greater than or equal to X which is K periodic (K <= N)
Examples:

Input: K = 2, X = “1215”
Output: 1313
Explanation:
1313 is 2-periodic since digits at positions 1, 3 are ‘1’ and digits at positions 2, 4 are ‘3’. This is the smallest 2-periodic integer greater than or equal to “1215”.
Input: K = 3, X = “299398”
Output: 300300
Explanation:
300300 is the smallest possible 3 periodic number.

Approach:
To solve the problem, the idea is to make Xi = Xi-k for all i > K. Then follow the steps below to solve the problem:

• Check if X greater than or equal to the initial integer. If so, then print the current string as the answer.
• Otherwise, traverse from right to left and find the first digit which is not equal to 9. Then increment that digit by 1 and mark the position by a variable called pos.
• Then set all digits after pos up to the Kth digit to 0. Again make Xi = Xi-k for all i >K.

Below is the implementation of the above approach:

## C++

 `// C++ Program to find the  ` `// smallest K periodic  ` `// integer greater than X  ` `#include   ` `using` `namespace` `std;  ` ` `  `// Function to find the  ` `// smallest K periodic  ` `// integer greater than X  ` `string Kperiodicinteger(string X, ``int` `N,  ` `                        ``int` `K)  ` `{  ` `    ``// Stores the number  ` `    ``// in a temporary string  ` `    ``string temp = X;  ` ` `  `    ``// Set X[i]=X[i-k] for  ` `    ``// i>k  ` `    ``for` `(``int` `i = 0; i < K; i++)  ` ` `  `    ``{  ` `        ``// Start from  ` `        ``// the current  ` `        ``// index  ` `        ``int` `j = i;  ` ` `  `        ``// Loop upto N  ` `        ``// change  ` `        ``// X[j] to X[i]  ` `        ``while` `(j < N) {  ` `            ``X[j] = X[i];  ` `            ``j += K;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Return X if current  ` `    ``// Value is greater  ` `    ``// than original value  ` `    ``if` `(X >= temp) {  ` ` `  `        ``return` `X;  ` `    ``}  ` ` `  `    ``int` `POS;  ` ` `  `    ``// Find the first  ` `    ``// digit not equal to 9  ` `    ``for` `(``int` `i = K - 1; i >= 0; i--) {  ` `        ``if` `(X[i] != ``'9'``) {  ` `             `  `        ``// Increment X[i]  ` `            ``X[i]++;  ` ` `  `            ``// Set POS to  ` `            ``// current index  ` `            ``POS = i;  ` ` `  `            ``break``;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Change X[i] to 0  ` `    ``// for all indices  ` `    ``// from POS+1 to K  ` `    ``for` `(``int` `i = POS + 1; i < K; i++) {  ` `        ``X[i] = ``'0'``;  ` `    ``}  ` ` `  `    ``// Set X[i]=X[i-k] for  ` `    ``// i>k  ` `    ``for` `(``int` `i = 0; i < K; i++)  ` ` `  `    ``{  ` `        ``int` `j = i;  ` `        ``// Loop upto N  ` `        ``// change  ` `        ``// X[j] to X[i]  ` ` `  `        ``while` `(j < N) {  ` `            ``X[j] = X[i];  ` `            ``j += K;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Return the  ` `    ``// final string  ` `    ``return` `X;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` ` `  `    ``int` `N = 4, K = 2;  ` ` `  `    ``string X = ``"1215"``;  ` ` `  `    ``cout << Kperiodicinteger(X, N, K);  ` ` `  `    ``return` `0;  ` `} `

## Java

 `// Java program to find the smallest  ` `// K periodic integer greater than X  ` `import` `java.util.*;  ` ` `  `class` `GFG{  ` ` `  `// Function to find the  ` `// smallest K periodic  ` `// integer greater than X  ` `static` `String Kperiodicinteger(``char` `[]X,  ` `                            ``int` `N, ``int` `K)  ` `{  ` `     `  `    ``// Stores the number  ` `    ``// in a temporary String  ` `    ``String temp = String.valueOf(X);  ` ` `  `    ``// Set X[i]=X[i-k] for  ` `    ``// i>k  ` `    ``for``(``int` `i = ``0``; i < K; i++)  ` `    ``{  ` `         `  `        ``// Start from the current  ` `        ``// index  ` `        ``int` `j = i;  ` ` `  `        ``// Loop upto N change  ` `        ``// X[j] to X[i]  ` `        ``while` `(j < N)  ` `        ``{  ` `            ``X[j] = X[i];  ` `            ``j += K;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Return X if current  ` `    ``// Value is greater  ` `    ``// than original value  ` `    ``if` `(String.valueOf(X).compareTo(temp) >= ``0``)  ` `    ``{  ` `        ``return` `String.valueOf(X);  ` `    ``}  ` ` `  `    ``int` `POS = ``0``;  ` ` `  `    ``// Find the first  ` `    ``// digit not equal to 9  ` `    ``for``(``int` `i = K - ``1``; i >= ``0``; i--)  ` `    ``{  ` `        ``if` `(X[i] != ``'9'``)  ` `        ``{  ` `             `  `            ``// Increment X[i]  ` `            ``X[i]++;  ` ` `  `            ``// Set POS to  ` `            ``// current index  ` `            ``POS = i;  ` `            ``break``;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Change X[i] to 0  ` `    ``// for all indices  ` `    ``// from POS+1 to K  ` `    ``for``(``int` `i = POS + ``1``; i < K; i++)  ` `    ``{  ` `        ``X[i] = ``'0'``;  ` `    ``}  ` ` `  `    ``// Set X[i]=X[i-k] for  ` `    ``// i>k  ` `    ``for``(``int` `i = ``0``; i < K; i++)  ` `    ``{  ` `        ``int` `j = i;  ` `         `  `        ``// Loop upto N change  ` `        ``// X[j] to X[i]  ` `        ``while` `(j < N)  ` `        ``{  ` `            ``X[j] = X[i];  ` `            ``j += K;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Return the  ` `    ``// final String  ` `    ``return` `String.valueOf(X);  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `N = ``4``, K = ``2``;  ` `    ``String X = ``"1215"``;  ` ` `  `    ``System.out.print(Kperiodicinteger(  ` `            ``X.toCharArray(), N, K));  ` `}  ` `}  ` ` `  `// This code is contributed by sapnasingh4991  `

## C#

 `// C# program to find the smallest ` `// K periodic integer greater than X ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to find the ` `// smallest K periodic ` `// integer greater than X ` `static` `String Kperiodicinteger(``char``[] X,  ` `                               ``int` `N, ``int` `K) ` `{ ` ` `  `    ``// Stores the number ` `    ``// in a temporary String ` `    ``String temp = String.Join(``""``, X); ` ` `  `    ``// Set X[i]=X[i-k] for ` `    ``// i>k ` `    ``for``(``int` `i = 0; i < K; i++) ` `    ``{ ` ` `  `        ``// Start from the current ` `        ``// index ` `        ``int` `j = i; ` ` `  `        ``// Loop upto N change ` `        ``// X[j] to X[i] ` `        ``while` `(j < N)  ` `        ``{ ` `            ``X[j] = X[i]; ` `            ``j += K; ` `        ``} ` `    ``} ` ` `  `    ``// Return X if current ` `    ``// Value is greater ` `    ``// than original value ` `    ``if` `(String.Join(``""``, X).CompareTo(temp) >= 0)  ` `    ``{ ` `        ``return` `String.Join(``""``, X); ` `    ``} ` ` `  `    ``int` `POS = 0; ` ` `  `    ``// Find the first ` `    ``// digit not equal to 9 ` `    ``for``(``int` `i = K - 1; i >= 0; i--)  ` `    ``{ ` `        ``if` `(X[i] != ``'9'``) ` `        ``{ ` ` `  `            ``// Increment X[i] ` `            ``X[i]++; ` ` `  `            ``// Set POS to ` `            ``// current index ` `            ``POS = i; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Change X[i] to 0 ` `    ``// for all indices ` `    ``// from POS+1 to K ` `    ``for``(``int` `i = POS + 1; i < K; i++) ` `    ``{ ` `        ``X[i] = ``'0'``; ` `    ``} ` ` `  `    ``// Set X[i]=X[i-k] for ` `    ``// i>k ` `    ``for``(``int` `i = 0; i < K; i++) ` `    ``{ ` `        ``int` `j = i; ` ` `  `        ``// Loop upto N change ` `        ``// X[j] to X[i] ` `        ``while` `(j < N) ` `        ``{ ` `            ``X[j] = X[i]; ` `            ``j += K; ` `        ``} ` `    ``} ` ` `  `    ``// Return the ` `    ``// readonly String ` `    ``return` `String.Join(``""``, X); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `N = 4, K = 2; ` `    ``String X = ``"1215"``; ` ` `  `    ``Console.Write(Kperiodicinteger( ` `          ``X.ToCharArray(), N, K)); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```1313
```

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : sapnasingh4991

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.