New update is available. Click here to update.
Close
Topic list
Ninja and Numbers
MEDIUM
25 mins
Strings
Topics (Covered in this problem)
Problem solved
Skill meter
Strings
-
-
Other topics
Problem solved
Skill meter
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Ninja and Numbers

Contributed by
Smit Mistry
Medium
0/80
Avg time to solve 25 mins
Success Rate 75 %
Share

## Problem Statement

#### Your task is to find the smallest number which is stable and whose value is greater than or equal to ‘A’. Zero-based indexing is used everywhere.

##### Example :
``````‘B’ = 4, ‘S’ = “4321”, ‘K’ = 3.
The given number is not stable as ‘S[3]’ is not the same as ‘S[0]’ but 3%3 = 0 same as 0%3. ‘S[3] = 1’ and ‘S[0] = 4’.  But the number “4324” is stable. As, for all ‘i’, ‘S[i]’ = ‘S[i%K]’ and “4324” is also greater than the given number. It can be proved that this is the best possible answer.
Hence, the answer is “4324”.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1 :
``````2
4 2
6825
3 3
420
``````
##### Sample Output 1 :
``````6868
420
``````
##### Explanation For Sample Input 1 :
``````For test case 1:
6868 is the minimum possible, stable number. We can see that it is stable because ‘S[0] = S[2]’ because 0%2 = 2%2, and ‘S[1] = S[3]’ because 1%2 = 3%2. All the conditions are satisfied.
Hence, 6868 is the answer.

For test case 2:
The given number itself is stable, and hence it is the best possible answer.
``````
##### Sample Input 2 :
``````2
5 1
40369
4 2
8516
``````
##### Sample Output 2 :
``````44444
8585
``````
Auto
Console