Statistics

Problem Statement for "RemoveDigits"

Problem Statement

Given a non-negative integer n (n may have up to 50 digits) and another number m, remove exactly m digits from n so the remaining number is as big as possible. m will always be less than the number of digits in n.

Create a class RemoveDigits containing the method maxNumber which takes a String n and an int m and returns a String containing the biggest number when removing exactly m digits from n.

Definition

Class:
RemoveDigits
Method:
maxNumber
Parameters:
String, int
Returns:
String
Method signature:
String maxNumber(String n, int m)
(be sure your method is public)

Notes

  • n may contain leading zeros, see example 1.
  • The return value should contain exactly |n|-m digits, where |n| is the number of digits in n.

Constraints

  • n will contain between 1 and 50 characters, inclusive.
  • n will only contain digits ('0'-'9').
  • m will be between 0 and |n|-1, inclusive, where |n| is the number of digits in n.

Examples

  1. "834391370"

    2

    Returns: "8491370"

    We should try to get the highest digits in the front. It's not possible to get the 9 in the front since that would require removing 4 digits, so we have to be satisfied with the 8. We still can't get the 9 to the second place in the final number, so the 4 is the highest choice for this position and thus we remove the digit 3. Now we can get the 9 as the third digit from the left in the final number by removing the second digit 3. Now we have removed 2 digits, and the number remaining is 8491370.

  2. "0021597002943020"

    3

    Returns: "2597002943020"

    We start with removing the two leading zeros and then the first 1, thus getting 2597002943020.

  3. "0000500390"

    2

    Returns: "00500390"

  4. "91459800928398113866079548895152924499"

    22

    Returns: "9999995152924499"

  5. "98"

    1

    Returns: "9"

  6. "5574742579580037796"

    5

    Returns: "77579580037796"

  7. "9"

    0

    Returns: "9"

  8. "97196906765509955148247685136485989590567213"

    24

    Returns: "99999888989590567213"

  9. "045976155811530869432464"

    21

    Returns: "996"

  10. "5042704197833986905959"

    3

    Returns: "5704197833986905959"

  11. "46015497067739966839147010850932314827724"

    6

    Returns: "97067739966839147010850932314827724"

  12. "837050534000993078357423071802503680066009"

    32

    Returns: "9988866009"

  13. "5913923747221068072017099345521753667"

    26

    Returns: "99995753667"

  14. "681"

    2

    Returns: "8"

  15. "421419323596316821426638394694"

    6

    Returns: "933596316821426638394694"

  16. "53629987"

    0

    Returns: "53629987"

  17. "97153"

    4

    Returns: "9"

  18. "07528682747096067186314583904499"

    4

    Returns: "8682747096067186314583904499"

  19. "51105949004125"

    2

    Returns: "515949004125"

  20. "4532805243631322601073622502317899126"

    28

    Returns: "877899126"

  21. "79419286675357073617180186979768641433121839086"

    29

    Returns: "999986443321839086"

  22. "27101821"

    0

    Returns: "27101821"

  23. "735674297930366516473023942133059"

    1

    Returns: "75674297930366516473023942133059"

  24. "858305306098129664314486"

    22

    Returns: "99"

  25. "203516274775843435431"

    1

    Returns: "23516274775843435431"

  26. "35173303638285773727812555"

    4

    Returns: "7333638285773727812555"

  27. "3519988270381272986302275"

    9

    Returns: "9988872986302275"

  28. "0053713395864221963086642659889255782345353"

    37

    Returns: "999985"

  29. "931327225382557685702445340295"

    0

    Returns: "931327225382557685702445340295"

  30. "91228486230828791783122"

    3

    Returns: "98486230828791783122"

  31. "5"

    0

    Returns: "5"

  32. "2260280513100029014888065551395552151441018"

    0

    Returns: "2260280513100029014888065551395552151441018"

  33. "8367212524761746221169322432511"

    13

    Returns: "877762269322432511"

  34. "9404726293910"

    4

    Returns: "976293910"

  35. "5906208116615"

    8

    Returns: "98665"

  36. "2760764491237019"

    7

    Returns: "791237019"

  37. "55878243502579215533780350587"

    2

    Returns: "878243502579215533780350587"

  38. "9218429133958991341631632306872134628"

    33

    Returns: "9999"

  39. "68967749393828728066"

    0

    Returns: "68967749393828728066"

  40. "525479"

    2

    Returns: "5579"

  41. "476479057050978798223962065952324"

    25

    Returns: "99999534"

  42. "86595156412"

    4

    Returns: "9556412"

  43. "460776853273467149443033352630411929436"

    17

    Returns: "9443033352630411929436"

  44. "982349234294189273489273418273489237438"

    38

    Returns: "9"

  45. "5390414380340543465274678742905860863658759366621"

    47

    Returns: "99"

  46. "0"

    0

    Returns: "0"

  47. "00"

    1

    Returns: "0"

  48. "789"

    2

    Returns: "9"

  49. "99999999999999999999999999999999999999999999999999"

    1

    Returns: "9999999999999999999999999999999999999999999999999"

  50. "987"

    1

    Returns: "98"

  51. "1337"

    0

    Returns: "1337"

  52. "123"

    0

    Returns: "123"

  53. "9999"

    1

    Returns: "999"

  54. "9999999999999999999999999999999999999999"

    5

    Returns: "99999999999999999999999999999999999"

  55. "001100"

    5

    Returns: "1"

  56. "999999"

    3

    Returns: "999"

  57. "1928374656473625278234"

    13

    Returns: "987778234"


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: