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.
Definition
- Class:
- MakeItDivisible
- Method:
- change
- Parameters:
- int
- Returns:
- int
- Method signature:
- int change(int N)
- (be sure your method is public)
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.
Examples
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.
0
Returns: 0
1
Returns: 0
2
Returns: 0
3
Returns: 0
4
Returns: 0
5
Returns: 0
6
Returns: 0
7
Returns: 7
8
Returns: 0
9
Returns: 0
10
Returns: 14
11
Returns: 14
12
Returns: 14
13
Returns: 14
14
Returns: 14
15
Returns: 14
16
Returns: 14
17
Returns: 77
18
Returns: 14
19
Returns: 14
20
Returns: 21
44
Returns: 42
61
Returns: 63
84
Returns: 84
124
Returns: 126
283
Returns: 280
290
Returns: 294
371
Returns: 371
380
Returns: 385
415
Returns: 413
421
Returns: 420
448
Returns: 448
460
Returns: 462
484
Returns: 483
544
Returns: 546
586
Returns: 581
604
Returns: 602
650
Returns: 651
694
Returns: 693
834
Returns: 833
9810442
Returns: 9810444
35718065
Returns: 35718060
247012036
Returns: 247012031
255274357
Returns: 255274355
259898950
Returns: 259898954
338849790
Returns: 338849791
385562480
Returns: 385562485
459239762
Returns: 459239760
474832180
Returns: 474832183
517064356
Returns: 517064352
551853547
Returns: 551853547
561857451
Returns: 561857450
587002530
Returns: 587002535
602311784
Returns: 602311780
609422724
Returns: 609422723
777237130
Returns: 777237132
850667235
Returns: 850667230
875864262
Returns: 875864262
965013962
Returns: 965013966
985055800
Returns: 985055806
999999997
Returns: 999999994
999999999
Returns: 999999994
49
Returns: 49
28
Returns: 28
999
Returns: 994