Statistics

Problem Statement for "TheLockDivTwo"

Problem Statement

John is obsessed with security. Recently he bought a new electronic lock. It is protected by a password containing n digits, where each digit is either zero or one. John decides to change the password every day. On the first day, the password is all zeroes. On each day that follows, he will select one or more digits that all have the same value and change their values (so zeroes become ones, and ones become zeroes). He must select the digits according to the following rules:

  1. During the first 2^n days, he must never use the same password twice.
  2. Each new password must come as early as possible alphabetically while not violating rule 1.

For example, if n is 2, the password on the first day is "00". The next day, he can change one or both 0's to get "01", "10" or "11". Of these possibilities, "01" comes earliest alphabetically. The next day, he can change either the 0 or the 1 to get "11" or "00". He can't choose "00" because it was already used, so he chooses "11". The next day, he can change one or both 1's to get "10", "01" or "00". He has already used "01" and "00", so he must choose "10".

Given ints n and k, return the password that John will choose on the k-th day (where k is a 1-based index).

Definition

Class:
TheLockDivTwo
Method:
password
Parameters:
int, int
Returns:
String
Method signature:
String password(int n, int k)
(be sure your method is public)

Notes

  • If A and B are two Strings of the same length, then A comes earlier alphabetically than B if it contains a smaller character at the first position where the Strings differ.

Constraints

  • n will be between 1 and 10, inclusive.
  • k will be between 1 and 2^n, inclusive.

Examples

  1. 2

    4

    Returns: "10"

    This is the example from the statement. The password sequence is the following - "00", "01", "11", "10".

  2. 4

    6

    Returns: "0100"

    "0000", "0001", "0011", "0010", "0110", "0100", ...

  3. 10

    1

    Returns: "0000000000"

    The password always consists of all zeroes on the first day.

  4. 7

    100

    Returns: "1100001"

  5. 10

    597

    Returns: "1001010001"

  6. 10

    15

    Returns: "0000001100"

  7. 10

    621

    Returns: "1001101000"

  8. 8

    249

    Returns: "11110110"

  9. 7

    117

    Returns: "1110001"

  10. 9

    442

    Returns: "110110100"

  11. 8

    228

    Returns: "11100000"

  12. 1

    1

    Returns: "0"

  13. 5

    15

    Returns: "01100"

  14. 5

    13

    Returns: "01010"

  15. 1

    1

    Returns: "0"

  16. 1

    1

    Returns: "0"

  17. 6

    32

    Returns: "011100"

  18. 10

    512

    Returns: "0111111001"

  19. 7

    64

    Returns: "0111110"

  20. 8

    128

    Returns: "01111010"

  21. 4

    8

    Returns: "0111"

  22. 2

    2

    Returns: "01"

  23. 3

    4

    Returns: "010"

  24. 10

    513

    Returns: "1111111001"

  25. 10

    513

    Returns: "1111111001"

  26. 6

    33

    Returns: "111100"

  27. 5

    17

    Returns: "11101"

  28. 10

    1024

    Returns: "1111111000"

  29. 7

    128

    Returns: "1111010"

  30. 7

    128

    Returns: "1111010"

  31. 10

    1024

    Returns: "1111111000"

  32. 10

    1

    Returns: "0000000000"

  33. 10

    110

    Returns: "0001101011"

  34. 9

    511

    Returns: "111111000"

  35. 10

    1023

    Returns: "1111111111"

  36. 8

    18

    Returns: "00010000"

  37. 10

    1011

    Returns: "1111101110"

  38. 10

    9

    Returns: "0000001111"

  39. 10

    987

    Returns: "1111010100"

  40. 9

    210

    Returns: "011001101"

  41. 9

    300

    Returns: "100101000"

  42. 10

    999

    Returns: "1111100001"

  43. 9

    120

    Returns: "001110110"

  44. 1

    2

    Returns: "1"

  45. 9

    510

    Returns: "111111111"

  46. 4

    5

    Returns: "0110"


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: