Statistics

Problem Statement for "MakeItDivisible"

Problem Statement

You are given a non-negative int N.

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

  1. 7028

    Returns: 7028

    This number is divisible by 7, so the optimal course of action is not to change anything.

  2. 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.

  3. 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".

  4. 1234567

    Returns: 1232567

  5. 3

    Returns: 0

    You may return either 0 or 7.

  6. 0

    Returns: 0

  7. 1

    Returns: 0

  8. 2

    Returns: 0

  9. 3

    Returns: 0

  10. 4

    Returns: 0

  11. 5

    Returns: 0

  12. 6

    Returns: 0

  13. 7

    Returns: 7

  14. 8

    Returns: 0

  15. 9

    Returns: 0

  16. 10

    Returns: 14

  17. 11

    Returns: 14

  18. 12

    Returns: 14

  19. 13

    Returns: 14

  20. 14

    Returns: 14

  21. 15

    Returns: 14

  22. 16

    Returns: 14

  23. 17

    Returns: 77

  24. 18

    Returns: 14

  25. 19

    Returns: 14

  26. 20

    Returns: 21

  27. 44

    Returns: 42

  28. 61

    Returns: 63

  29. 84

    Returns: 84

  30. 124

    Returns: 126

  31. 283

    Returns: 280

  32. 290

    Returns: 294

  33. 371

    Returns: 371

  34. 380

    Returns: 385

  35. 415

    Returns: 413

  36. 421

    Returns: 420

  37. 448

    Returns: 448

  38. 460

    Returns: 462

  39. 484

    Returns: 483

  40. 544

    Returns: 546

  41. 586

    Returns: 581

  42. 604

    Returns: 602

  43. 650

    Returns: 651

  44. 694

    Returns: 693

  45. 834

    Returns: 833

  46. 9810442

    Returns: 9810444

  47. 35718065

    Returns: 35718060

  48. 247012036

    Returns: 247012031

  49. 255274357

    Returns: 255274355

  50. 259898950

    Returns: 259898954

  51. 338849790

    Returns: 338849791

  52. 385562480

    Returns: 385562485

  53. 459239762

    Returns: 459239760

  54. 474832180

    Returns: 474832183

  55. 517064356

    Returns: 517064352

  56. 551853547

    Returns: 551853547

  57. 561857451

    Returns: 561857450

  58. 587002530

    Returns: 587002535

  59. 602311784

    Returns: 602311780

  60. 609422724

    Returns: 609422723

  61. 777237130

    Returns: 777237132

  62. 850667235

    Returns: 850667230

  63. 875864262

    Returns: 875864262

  64. 965013962

    Returns: 965013966

  65. 985055800

    Returns: 985055806

  66. 999999997

    Returns: 999999994

  67. 999999999

    Returns: 999999994

  68. 49

    Returns: 49

  69. 28

    Returns: 28

  70. 999

    Returns: 994


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: