Visa OA Interview Question: Counting Game

15 Views
No Comments

Counting Game

The counting game is a widely popular casual game. Every participant in the game must count numbers in sequence. However, if the next number to be called is a multiple of 7, or if the next number contains the digit 7, then that number must be skipped; otherwise, you lose the game.

Rose and Zack play this game and find it too easy, so they decide to modify some rules: for any number containing the digit 7, all its multiples cannot be called either.

For example, if Rose calls out 6, since 7 cannot be called, Zack must call 8 next. If Rose calls out 33, since 34 is 17 times 2 and 35 is 7 times 5, Zack’s next possible call is 36. If Rose calls out 69, because numbers 70 to 79 all contain the digit 7, Zack’s next call must be 80.

Input Format

Input a line which contains a positive integer X, indicating the number called by Rose this time.

Output Format

Output a line which contains an integer.

If the number called by Rose this time is invalid (cannot be called), output -1. Otherwise, output the number that Zack should call next.

Sample Input 1

6

Sample Output 1

8

Sample Input 2

33

Sample Output 2

36

This problem asks for the next valid number in a modified counting game. A number is invalid if it contains digit 7, or if it is a multiple of any number that contains digit 7. The solution is straightforward: first verify whether X itself is valid; if not, return -1. Otherwise, scan upward from X + 1 until the first valid number is found. The key implementation detail is a correct validity check for both the digit rule and the multiple rule.

END
 0