Numbers
InterviewTime limit1sMemory limit1024 MB
Given x and a flag k, find the smallest y at least x whose digits are all equal, or all equal except one digit when k is 1.
- Level
Medium4 of 10
- Topics
- Implementation, Brute force, Math
- Solved
- No attempts yet
Problem
Darina likes numbers whose digits are all the same. For example, she likes or , but she does not like at all. When she is in a good mood, she also likes numbers in which all digits except one are the same, such as .
Darina has a number . She wants to find the smallest integer that she likes, meaning either all of its digits are the same, or only one of its digits is different from the others.
Write a program numbers that, given an integer and whether Darina is in a good mood, finds the smallest integer that Darina likes.
Input
The first line of standard input contains one integer . The second line contains one integer , which is or . The value means Darina is in a good mood, and means she is not.
Output
The first line of standard output contains one integer that satisfies the following conditions:
- .
- If , all digits of are the same.
- If , all digits of are the same, except possibly one digit.
Constraints
.