Statistics

Problem Statement for "ConsecutiveOnes"

Problem Statement

You are given a long n. You are also given an int k that is a positive integer between 1 and 50, inclusive. Find and return the smallest m such that:
  • m is greater than or equal to n
  • the binary representation of m contains (at least) k consecutive ones

Definition

Class:
ConsecutiveOnes
Method:
get
Parameters:
long, int
Returns:
long
Method signature:
long get(long n, int k)
(be sure your method is public)

Constraints

  • n will be beween 0 and 2^50 - 1, inclusive.
  • k will be between 1 and 50, inclusive.

Examples

  1. 1

    2

    Returns: 3

    We want the smallest integer that is at least 1 and contains 2 consecutive ones in binary. Clearly the smallest such integer is 3.

  2. 5

    2

    Returns: 6

    The binary representation of the number 5 is 101, which does not contain two consecutive ones. The next integer is 6, which is 110 in binary. As this does contain two consecutive ones, the correct return value is 6.

  3. 7

    2

    Returns: 7

  4. 1023

    11

    Returns: 2047

  5. 364269800189924

    33

    Returns: 364273356242943

  6. 437001103351396

    10

    Returns: 437001103351800

  7. 137344374592196

    50

    Returns: 1125899906842623

  8. 963587552675620

    49

    Returns: 1125899906842622

  9. 663926528949124

    39

    Returns: 664105023176696

  10. 64613071430724

    36

    Returns: 64665027608575

  11. 296840970058165

    10

    Returns: 296840970058751

  12. 183881129413730

    1

    Returns: 183881129413730

  13. 693491171995104

    50

    Returns: 1125899906842623

  14. 221937368364725

    45

    Returns: 246290604621823

  15. 149206065203253

    49

    Returns: 562949953421311

  16. 396002446800575

    20

    Returns: 396002447065087

  17. 495163923542986

    30

    Returns: 495164632072190

  18. 38418523090786

    9

    Returns: 38418523090936

  19. 338075251849986

    8

    Returns: 338075251850236

  20. 637736275576482

    7

    Returns: 637736275576574

  21. 937397299302978

    6

    Returns: 937397299303038

  22. 111149826252258

    5

    Returns: 111149826252272

  23. 410810849978754

    4

    Returns: 410810849978767

  24. 710467578737954

    3

    Returns: 710467578737954

  25. 1010128602464450

    2

    Returns: 1010128602464450

  26. 1123079853174293

    11

    Returns: 1123079853174782

  27. 823423124415093

    12

    Returns: 823423124418559

  28. 523766395655893

    13

    Returns: 523766395658239

  29. 224109666896693

    14

    Returns: 224109666910207

  30. 1050348550012821

    15

    Returns: 1050348550029310

  31. 750687526286325

    16

    Returns: 750687526322168

  32. 1125899890065407

    25

    Returns: 1125899890065407

  33. 7176

    3

    Returns: 7176

  34. 0

    50

    Returns: 1125899906842623

  35. 1

    50

    Returns: 1125899906842623

  36. 50

    2

    Returns: 50

  37. 269

    2

    Returns: 269

  38. 56

    3

    Returns: 56

  39. 44

    2

    Returns: 44

  40. 13

    2

    Returns: 13

  41. 2040

    2

    Returns: 2040

  42. 6334

    1

    Returns: 6334

  43. 17583619156951

    11

    Returns: 17583619156951

  44. 61

    4

    Returns: 61

  45. 12

    2

    Returns: 12

  46. 443

    4

    Returns: 444

  47. 0

    1

    Returns: 1

  48. 1113074019211794

    1

    Returns: 1113074019211794

  49. 24

    3

    Returns: 28

  50. 186

    4

    Returns: 188

  51. 950517515131926

    4

    Returns: 950517515131926

  52. 25

    50

    Returns: 1125899906842623

  53. 249

    6

    Returns: 252

  54. 256720229

    2

    Returns: 256720229

  55. 650073

    3

    Returns: 650073

  56. 1125899890065407

    32

    Returns: 1125899906580480


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: