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).
Input: K = 2, X = “1215”
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”
300300 is the smallest possible 3 periodic number.
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:
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.
- Smallest number greater than Y with sum of digits equal to X
- Count the number of words having sum of ASCII values less than and greater than k
- Largest number not greater than N which can become prime after rearranging its digits
- Smallest subset with sum greater than all other elements
- Smallest Greater (than S) String of length K whose letters are subset of S
- Smallest subarray having an element with frequency greater than that of other elements
- Smallest alphabet greater than a given character
- Count of alphabets having ASCII value less than and greater than k
- Find words which are greater than given length k
- Find words which are greater than given length k using stringstream
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Count of array elements which are greater than all elements on its left
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Count number of substrings with numeric value greater than X
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Number of pairs in an array with the sum greater than 0
- Maximize the number of indices such that element is greater than element to its left
- Split Array into min number of subsets with difference between each pair greater than 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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