Find Kth good number greater than N
Filed under BNY Mellon
Free read 2 of 3Confirm if N is given as an integer or string, and the expected range for K.
The question
Find the Kth 'good' number strictly greater than a given number N, where a good number is defined as having a digit sum divisible by 5.
Constraints
- N can be up to 10^18.
- K is a small positive integer.
- Time limit requires efficient searching beyond linear scan.
Examples
- Input
- N = 4, K = 1
- Output
- 5
- Why
- Numbers greater than 4 with digit sum divisible by 5: 5 (5%5==0), 9 (9%5!=0), etc. First is 5.
Approach
Confirm if N is given as an integer or string, and the expected range for K.
Loop from N+1 upwards, checking each number's digit sum until K good numbers are found.
Use binary search combined with Digit DP counting to find the boundary, or generate numbers structurally.
N close to max integer limits, K=1.
Tips
- What they are testing: Test boundary conditions where N itself has a digit sum divisible by 5 (remember we want numbers strictly greater than N).
- Common mistake: Incrementing N one by one in a loop, which will time out for large N.
Related BNY Mellon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.