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:
- During the first 2^n days, he must never use the same password twice.
- 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
Definition
- Class:
- TheLockDivOne
- Method:
- password
- Parameters:
- int, long
- Returns:
- String
- Method signature:
- String password(int n, long 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 50, inclusive.
- k will be between 1 and 2^n, inclusive.
Examples
2
4
Returns: "11"
This is the example from the statement. The password sequence is the following - "00", "01", "11", "10".
3
8
Returns: "111"
"000", "001", "011", "010", "110", "100", "101", "111".
4
6
Returns: "0110"
10
1
Returns: "0000000000"
10
597
Returns: "1111111001"
10
15
Returns: "0000001111"
10
621
Returns: "1111111001"
8
249
Returns: "11111110"
7
117
Returns: "1111110"
9
442
Returns: "111111011"
8
228
Returns: "11111110"
1
1
Returns: "0"
5
15
Returns: "01111"
5
13
Returns: "01111"
1
1
Returns: "0"
1
1
Returns: "0"
6
32
Returns: "011111"
10
512
Returns: "0111111111"
7
64
Returns: "0111111"
8
128
Returns: "01111111"
4
8
Returns: "0111"
2
2
Returns: "01"
3
4
Returns: "011"
10
513
Returns: "1111111001"
10
513
Returns: "1111111001"
6
33
Returns: "111100"
5
17
Returns: "11101"
10
1024
Returns: "1111111111"
7
128
Returns: "1111111"
7
128
Returns: "1111111"
10
1024
Returns: "1111111111"
10
1
Returns: "0000000000"
48
208512386748577
Returns: "111111111111111111111111111111111111111111010100"
25
27411233
Returns: "1111111111111111111101110"
38
105246396407
Returns: "01111111111111111111111111111111100001"
23
6621849
Returns: "11111111111111111101101"
45
11398125475641
Returns: "011111111111111111111111111111111111111011000"
23
6380913
Returns: "11111111111111111101101"
34
3005954651
Returns: "0011111111111111111111111111100100"
50
1052937316880545
Returns: "11111111111111111111111111111111111111111111010110"
50
75288321082145
Returns: "00011111111111111111111111111111111111111111010101"
50
174927595212791
Returns: "00111111111111111111111111111111111111111111010100"
50
282406622136985
Returns: "01111111111111111111111111111111111111111111010110"
50
562949953421312
Returns: "01111111111111111111111111111111111111111111111111"
50
1125899906842624
Returns: "11111111111111111111111111111111111111111111111111"
50
562949953421313
Returns: "11111111111111111111111111111111111111111111010010"
50
1
Returns: "00000000000000000000000000000000000000000000000000"
34
2719886102
Returns: "0011111111111111111111111111100100"
31
41952292
Returns: "0000011111111111111111111101011"
32
19008497
Returns: "00000001111111111111111111101010"
49
93536637109166
Returns: "0011111111111111111111111111111111111111111010101"
7
100
Returns: "1111110"
50
725899906842624
Returns: "11111111111111111111111111111111111111111111010010"
50
123456789012345
Returns: "00011111111111111111111111111111111111111111011111"
50
1025899906842619
Returns: "11111111111111111111111111111111111111111111010110"
50
543797347268423
Returns: "01111111111111111111111111111111111111111111010111"
50
281474976710657
Returns: "01111111111111111111111111111111111111111111010110"
50
33553333
Returns: "00000000000000000000000001111111111111111111111111"
50
562949953131313
Returns: "01111111111111111111111111111111111111111111111101"
49
3214659873999
Returns: "0000000111111111111111111111111111111111111011011"
50
140737488355328
Returns: "00011111111111111111111111111111111111111111111111"
50
100000000000000
Returns: "00011111111111111111111111111111111111111111010101"
50
1234565676543
Returns: "00000000011111111111111111111111111111111111011010"
50
1000000000000000
Returns: "11111111111111111111111111111111111111111111010110"
49
562946180435304
Returns: "1111111111111111111111111111111111111111111111100"
50
562949953421666
Returns: "11111111111111111111111111111111111111111111010010"
20
513
Returns: "00000000001111111001"
50
365487745125434
Returns: "01111111111111111111111111111111111111111111010110"
50
27
Returns: "00000000000000000000000000000000000000000000011111"
50
1125899871101813
Returns: "11111111111111111111111111111111111111111111111100"
49
35184372088832
Returns: "0000111111111111111111111111111111111111111111111"
50
745493936856584
Returns: "11111111111111111111111111111111111111111111010010"
20
1025
Returns: "00000000011111111000"
49
562949953421000
Returns: "1111111111111111111111111111111111111111111111111"