Skip to content
Related Articles

Related Articles

Improve Article
Smallest number greater than X which is K-periodic
  • Last Updated : 31 May, 2021

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 <bits/stdc++.h>
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

Python3




# Python3 program to find the smallest
# K periodic integer greater than X
 
# Function to find the 
# smallest K periodic 
# integer greater than X
def Kperiodicinteger(X, N, K):
     
    X = list(X)
     
    # Stores the number 
    # in a temporary string 
    temp = X.copy()
 
    # Set X[i]=X[i-k] for 
    # i>k
    for i in range(K):
         
        # Start from the current 
        # index 
        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
 
    POS = 0
 
    # Find the first digit
    # not equal to 9
    for i in range(K - 1, -1, -1):
        if (X[i] != '9'):
             
            # Increment X[i] 
            X[i] = str(int(X[i]) + 1)
 
            # Set POS to 
            # current index
            POS = i
            break
 
    # Change X[i] to 0 
    # for all indices 
    # from POS+1 to K 
    for i in range(POS + 1, K):
        X[i] = '0'
 
    # Set X[i]=X[i-k] for 
    # i>k
    for i in range(K):
        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
N = 4
K = 2
X = "1215"
 
print(*Kperiodicinteger(X, N, K), sep = '')
         
# This code is contributed by avanitrachhadiya2155

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

Javascript




<script>
 
// Javascript Program to find the
// smallest K periodic
// integer greater than X
 
// Function to find the
// smallest K periodic
// integer greater than X
function Kperiodicinteger(X, N, K)
{
    // Stores the number
    // in a temporary string
    var temp = X
 
    // Set X[i]=X[i-k] for
    // i>k
    for (var i = 0; i < K; i++)
 
    {
        // Start from
        // the current
        // index
        var 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 (parseInt(X.join('')) < parseInt(temp.join(''))) {
 
        return X.join('');
    }
 
    var POS;
 
    // Find the first
    // digit not equal to 9
    for (var i = K - 1; i >= 0; i--) {
        if (X[i] != '9') {
             
        // Increment X[i]
            X[i] = String.fromCharCode(X[i].charCodeAt(0) + 1);
 
            // Set POS to
            // current index
            POS = i;
 
            break;
        }
    }
 
    // Change X[i] to 0
    // for all indices
    // from POS+1 to K
    for (var i = POS + 1; i < K; i++) {
        X[i] = '0';
    }
 
    // Set X[i]=X[i-k] for
    // i>k
    for (var i = 0; i < K; i++)
 
    {
        var 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.join('');
}
 
// Driver Code
var N = 4, K = 2;
var X = "1215";
document.write( Kperiodicinteger(X.split(''), N, K));
 
// This code is contributed by itsok.
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :