Statistics

Problem Statement for "NextNumber"

Problem Statement

The binary weight of a positive integer is the number of 1's in its binary representation. For example, the decimal number 1 has a binary weight of 1, and the decimal number 1717 (which is 11010110101 in binary) has a binary weight of 7.

Given a positive integer N, return the smallest integer greater than N that has the same binary weight as N.

Definition

Class:
NextNumber
Method:
getNextNumber
Parameters:
int
Returns:
int
Method signature:
int getNextNumber(int N)
(be sure your method is public)

Notes

  • The result is guaranteed to fit in a signed 32-bit integer.

Constraints

  • N will be between 1 and 1,000,000,000, inclusive.

Examples

  1. 1717

    Returns: 1718

    Example from the problem statement.

  2. 1

    Returns: 2

  3. 2

    Returns: 4

  4. 4

    Returns: 8

    4 is 100 in its binary representation and weighs 1. The next number is 1000(in binary) which represents 8.

  5. 1024

    Returns: 2048

  6. 1048576

    Returns: 2097152

  7. 268435456

    Returns: 536870912

  8. 536870910

    Returns: 671088639

  9. 536870911

    Returns: 805306367

  10. 536870912

    Returns: 1073741824

  11. 805306368

    Returns: 1073741825

  12. 973078527

    Returns: 989855743

  13. 805306367

    Returns: 939524095

  14. 939524096

    Returns: 1073741827

  15. 268435454

    Returns: 335544319

  16. 268435455

    Returns: 402653183

  17. 500000000

    Returns: 500000256

  18. 800000000

    Returns: 800002048

  19. 900000000

    Returns: 900000256

  20. 999990000

    Returns: 999990023

  21. 999999999

    Returns: 1000000255

  22. 1000000000

    Returns: 1000000512

  23. 7

    Returns: 11

    The decimal 7 is binary 111, so it has binary weight of 3. The next number with the same binary weight is 11, which is 1011 in binary.

  24. 12

    Returns: 17

    12 in decimal is 1100 in binary. The next number with the same binary weight is 10001 in binary, which is 17.

  25. 555555

    Returns: 555557

  26. 469762048

    Returns: 536870915

  27. 54

    Returns: 57

  28. 44

    Returns: 49

  29. 33554432

    Returns: 67108864

  30. 22

    Returns: 25

  31. 249209942

    Returns: 249209945

  32. 402653184

    Returns: 536870913

  33. 7663672

    Returns: 7663683

  34. 671088640

    Returns: 805306368

  35. 5354378

    Returns: 5354380

  36. 67108864

    Returns: 134217728

  37. 949209942

    Returns: 949209945

  38. 268419072

    Returns: 268443647

  39. 555555678

    Returns: 555555687

  40. 60

    Returns: 71

  41. 805240880

    Returns: 805240897

  42. 94

    Returns: 103

  43. 46

    Returns: 51

  44. 88

    Returns: 97

  45. 536854528

    Returns: 536887295

  46. 80

    Returns: 96

  47. 15

    Returns: 23

  48. 134217727

    Returns: 201326591

  49. 1718

    Returns: 1721

  50. 24

    Returns: 33

  51. 805306364

    Returns: 838860799

  52. 348

    Returns: 355

  53. 92

    Returns: 99

  54. 28

    Returns: 35

  55. 6482136

    Returns: 6482145

  56. 924

    Returns: 931

  57. 76

    Returns: 81

  58. 36

    Returns: 40

  59. 1984

    Returns: 2063

  60. 1564

    Returns: 1571

  61. 316

    Returns: 327

  62. 184

    Returns: 195

  63. 992

    Returns: 1039


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: