MakeItDivisible
SRM 822 · 2022-01-16 · by misof
Problem Statement
You are given a non-negative
You want an integer that is divisible by 7. If N is already divisible by 7, you are happy and you don't need to change it.
If N isn't divisible by 7, you want to change it to a multiple of 7 by changing as few digits of N as possible.
Make those changes and return the new number. Any valid solution will be accepted.
Notes
- You are only allowed to change digits, not to add or remove them. Thus, the number you return must always have the same number of digits as the number N you are given.
Constraints
- N will be between 0 and 999,999,999, inclusive.
7028 Returns: 7028
This number is divisible by 7, so the optimal course of action is not to change anything.
1111 Returns: 1113
This number is not divisible by 7. There are five ways to turn it into a multiple of 7 by changing just a single digit: you can return any of the numbers 1113, 1141, 1211, 1911, and 6111. The number 1127 is divisible by 7, but this number is not a valid answer: one needs to change two digits to produce it from 1111. The number 41111 is divisible by 7, but it is also not a valid answer: you are not allowed to add digits.
17 Returns: 77
You may change this number either to 77 (changing the first digit from 1 to 7) or to 14 (changing the second digit from 7 to 4). Note that you cannot change the first digit from 1 to 0. The new number cannot have unnecessary leading zeros. You cannot return "07", and you cannot return "7" because it doesn't have the same number of digits as "17".
1234567 Returns: 1232567
3 Returns: 0
You may return either 0 or 7.
Submissions are judged against all 70 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MakeItDivisible with a public method int change(int N) · 70 test cases · 2 s / 256 MB per case