Ctrl K

A Multiple Spelled With Two Digits

Medium
You are given a number base and two digits first_digit and second_digit (they may be equal). Find the smallest integer that: - is strictly greater than base; - is divisible by base; - is written using only the digits first_digit and second_digit (either one or both). If no such number exists, or it is greater than 2³¹ - 1, return -1.
Example 1
base=6, first_digit=1, second_digit=2 → 12

Numbers made of 1 and 2 in increasing order: 1, 2, 11, 12, ... and 12 is above 6 and divisible by 6.

Example 2
base=7, first_digit=7, second_digit=0 → 70

7 itself does not count, since the answer must be strictly greater. The next fit is 70.

Example 3
base=4, first_digit=5, second_digit=5 → -1

Numbers made only of 5s are odd and never divisible by 4.

Constraints
  • 1 ≤ base ≤ 1000
  • 0 ≤ first_digit, second_digit ≤ 9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrimes Below a Bound MediumShuffle the Digits Into a Power of Two MediumSums of Two Powers MediumMicrowave Keypad Effort MediumNotebooks and Markers Medium
loading Python…
Input
base=6, first_digit=1, second_digit=2
Expected
12

Run checks the first 3 tests in your browser. Submit checks all 118 tests on the server, hidden ones included.