Statistics

Problem Statement for "NumbersLine"

Problem Statement

You are given a String line containing a list of integers, each separated by one or more spaces. Given an int givenNumber, return the smallest int in line that is strictly greater than givenNumber. Return -1 if there is no such number in line.

Definition

Class:
NumbersLine
Method:
getLeast
Parameters:
String, int
Returns:
int
Method signature:
int getLeast(String line, int givenNumber)
(be sure your method is public)

Notes

  • line may contain leading and/or trailing spaces.

Constraints

  • line will contain between 1 and 50 characters, inclusive.
  • Each character in line will be either a digit ('0'-'9') or a space (' ').
  • line will contain a list of integers, separated by one or more spaces, where each integer is between 1 and 1000, inclusive, with no leading zeros.
  • line will contain at least one integer.
  • givenNumber will be between 1 and 1000, inclusive.

Examples

  1. "1 2 3 4 5"

    2

    Returns: 3

    3 is the smallest number strictly greater than 2.

  2. "120 450 780"

    1000

    Returns: -1

    There are no numbers strictly greater than 1000.

  3. " 45 253 645 400 676 567 "

    1

    Returns: 45

  4. " 45 253 645 400 676 567 "

    400

    Returns: 567

  5. " 568 769 436 432 457 563 567 311 34 3 2 9"

    460

    Returns: 563

  6. "2"

    1

    Returns: 2

  7. " 2 "

    2

    Returns: -1

  8. "2"

    3

    Returns: -1

  9. " 1000 "

    1000

    Returns: -1

  10. " 1000"

    12

    Returns: 1000

  11. " 345 4"

    5

    Returns: 345

  12. "25 43 346 "

    1

    Returns: 25

  13. " 1000 "

    999

    Returns: 1000

  14. " 4 7 3 1"

    1

    Returns: 3

  15. " 43 234 23 1 32 "

    2

    Returns: 23

  16. " 5 5 5 "

    2

    Returns: 5

  17. " 5 5 5 "

    5

    Returns: -1

  18. " 5 5 5 "

    90

    Returns: -1

  19. " 45"

    34

    Returns: 45

  20. "54 "

    368

    Returns: -1

  21. "5 4 5 7 8 3 4 5 7 9 4 2 5 8 4 1 5 8 43 243 33 57 9"

    3

    Returns: 4

  22. "341 740 "

    490

    Returns: 740

  23. "790 872 751 779 39 "

    213

    Returns: 751

  24. "137 284 474 "

    829

    Returns: -1

  25. "395 133 552 636 974 852 384 335 472 335 560 "

    135

    Returns: 335

  26. "558 147 688 "

    727

    Returns: -1

  27. "309 409 774 92 804 103 11 655 679 36 378 "

    152

    Returns: 309

  28. "980 222 "

    26

    Returns: 222

  29. "642 735 358 589 513 226 625 813 609 "

    819

    Returns: -1

  30. "493 557 45 209 966 835 176 219 297 669 "

    389

    Returns: 493

  31. " 899 868 903 333 32 561 920 "

    414

    Returns: 561

  32. "526 791 671 999 489 635 104 628 "

    545

    Returns: 628

  33. "3 196 460 748 501 "

    143

    Returns: 196

  34. "78 369 6 "

    285

    Returns: 369

  35. " 731 672 698 48 716 329 489 616 510 493 176 "

    803

    Returns: -1

  36. " 20 203 258 781 115 104 607 628 "

    653

    Returns: 781

  37. "449 977 621 275 269 125 41 418 175 219 "

    785

    Returns: 977

  38. "681 "

    843

    Returns: -1

  39. "977 "

    596

    Returns: 977

  40. "489 446 773 327 114 902 "

    821

    Returns: 902

  41. " 7 425 733 79 500 240 367 371 295 391 36 "

    647

    Returns: 733

  42. "3"

    1

    Returns: 3

  43. "120 450 780"

    1000

    Returns: -1

  44. "1 2 3 57"

    56

    Returns: 57

  45. " 45 253 645 400 676 567"

    400

    Returns: 567

  46. "1 2 3 4 5"

    100

    Returns: -1

  47. "1 3"

    2

    Returns: 3

  48. "3 1"

    2

    Returns: 3

  49. "1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5"

    2

    Returns: 3


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: