Statistics

Problem Statement for "BinaryIncrementation"

Problem Statement

The binary numeral system (base 2 numerals) represents numeric values using two symbols: 0 and 1. Counting in binary is similar to counting in any other number system. If you want to increase a number by 1, try to increase its last digit by 1. If this fails, set the last digit to zero, and try to increase the previous digit, and so on, until you succeed.

For example, the decimal sequence:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
converted to binary looks as follows:
1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, ...

You are given a String x that contains the binary representation of a positive integer X. Write a method that will return a String containing the binary representation of (X+1). The returned String must not contain leading zeroes.

Definition

Class:
BinaryIncrementation
Method:
plusOne
Parameters:
String
Returns:
String
Method signature:
String plusOne(String x)
(be sure your method is public)

Constraints

  • x will contain between 1 and 30 characters, inclusive.
  • Each character in x will be a one ('1') or a zero ('0').
  • The first character in x will be a one ('1').

Examples

  1. "10011"

    Returns: "10100"

    "10011" is binary for 16+2+1 = 19. The next integer is 20 = 16+4, which is "10100" in binary.

  2. "10000"

    Returns: "10001"

  3. "1111"

    Returns: "10000"

    Be careful not to miss the case when the result is a power of two.

  4. "1"

    Returns: "10"

  5. "101010101010101010101010101010"

    Returns: "101010101010101010101010101011"

  6. "111111111111111111111111111111"

    Returns: "1000000000000000000000000000000"

  7. "10"

    Returns: "11"

  8. "11"

    Returns: "100"

  9. "100"

    Returns: "101"

  10. "101"

    Returns: "110"

  11. "110"

    Returns: "111"

  12. "111"

    Returns: "1000"

  13. "1000"

    Returns: "1001"

  14. "1001"

    Returns: "1010"

  15. "1010"

    Returns: "1011"

  16. "1011"

    Returns: "1100"

  17. "100101010101010000"

    Returns: "100101010101010001"

  18. "101111111111111111111111111111"

    Returns: "110000000000000000000000000000"

  19. "100000000000000000000000000000"

    Returns: "100000000000000000000000000001"

  20. "100000000000011000000000000001"

    Returns: "100000000000011000000000000010"

  21. "100001111110111111011111"

    Returns: "100001111110111111100000"

  22. "100001"

    Returns: "100010"

  23. "111111111111111111111111111111"

    Returns: "1000000000000000000000000000000"

  24. "1111"

    Returns: "10000"

  25. "100101"

    Returns: "100110"

  26. "10001"

    Returns: "10010"

  27. "11111"

    Returns: "100000"

  28. "10011"

    Returns: "10100"

  29. "1"

    Returns: "10"


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: