Problem Statement
Consider the implementation of binary search shown below:
def binary_search(array, value): low = 0 high = len(array)-1 while low <= high: mid = (low + high) / 2 if array[mid] == value: return '1' if array[mid] < value: low = mid + 1 if array[mid] > value: high = mid - 1 return '0'
Clearly, if the input array is sorted, the procedure returns '1' if the given value is present in the given array and it returns '0' if the value is not in the array.
In this problem we'll take a look at what happens if the array isn't sorted. Even in those cases binary search can sometimes give the correct answer, but sometimes we can search for a value that is present in the array and the search will fail to find it.
Let P be a permutation of numbers from 0 to N-1, inclusive.
The signature of P is a string sig(P) of length N defined as follows:
sig(P) = binary_search(P,0) + binary_search(P,1) + ... + binary_search(P,N-1)
In words, character i of the signature of a permutation tells us whether binary search correctly locates the value i in this permutation.
You are given a
Definition
- Class:
- BinarySearch
- Method:
- count
- Parameters:
- String
- Returns:
- int
- Method signature:
- int count(String S)
- (be sure your method is public)
Notes
- In the pseudocode for binary search, the valid indices into an array of length N are values from 0 to N-1, inclusive.
- Also in the pseudocode, "/" represents integer division that truncates (e.g., 9/2 = 4).
Constraints
- S will have between 1 and 50 characters, inclusive.
- Each character of S will be either '0' or '1'.
Examples
"111"
Returns: 1
There is only one permutation with this signature: P = {0, 1, 2}. In other words, for N = 3 binary search always works if and only if the input permutation is sorted in increasing order.
"000"
Returns: 0
There is no permutation with this signature. I.e., for N = 3 there is no permutation such that binary search always fails.
"101"
Returns: 2
The two permutations with this signature are P = {0, 2, 1} and P = {1, 0, 2}.
"100011"
Returns: 12
"0011101100010010010111010111010"
Returns: 373885200
"0"
Returns: 0
"1"
Returns: 1
"00"
Returns: 0
"10"
Returns: 0
"01"
Returns: 1
"11"
Returns: 1
"100"
Returns: 0
"010"
Returns: 1
"110"
Returns: 1
"001"
Returns: 0
"011"
Returns: 1
"0000"
Returns: 0
"1000"
Returns: 0
"0100"
Returns: 0
"1100"
Returns: 0
"0010"
Returns: 2
"1010"
Returns: 1
"0110"
Returns: 2
"1110"
Returns: 1
"0001"
Returns: 0
"1001"
Returns: 4
"0101"
Returns: 4
"1101"
Returns: 2
"0011"
Returns: 2
"1011"
Returns: 3
"0111"
Returns: 2
"1111"
Returns: 1
"00000"
Returns: 0
"10000"
Returns: 0
"01000"
Returns: 0
"11000"
Returns: 0
"00100"
Returns: 4
"10100"
Returns: 2
"01100"
Returns: 4
"11100"
Returns: 2
"00010"
Returns: 6
"10010"
Returns: 6
"01010"
Returns: 7
"11010"
Returns: 3
"00110"
Returns: 6
"10110"
Returns: 4
"01110"
Returns: 3
"11110"
Returns: 1
"00001"
Returns: 0
"10001"
Returns: 6
"01001"
Returns: 8
"11001"
Returns: 6
"00101"
Returns: 8
"10101"
Returns: 6
"01101"
Returns: 6
"11101"
Returns: 2
"00011"
Returns: 6
"10011"
Returns: 4
"01011"
Returns: 5
"11011"
Returns: 3
"00111"
Returns: 6
"10111"
Returns: 2
"01111"
Returns: 3
"11111"
Returns: 1
"11111111111111111111111111111111111111111111111111"
Returns: 1
"00000000000000000000000000000000000000000000000000"
Returns: 0
"00000000000000000010100000100000001010100000000000"
Returns: 805159173
"01001100000010000000010010010000000000000011010000"
Returns: 284574371
"01000010100000110000011000010000010101000110000000"
Returns: 108757553
"11110000001110000011111110110101110001101011000000"
Returns: 754590874
"00111010001011101001000110100001010100011111010010"
Returns: 169562051
"11010011110111011110000001101101000010001101001101"
Returns: 101750443
"11111111001111011111001001111001100101101010101111"
Returns: 253593247
"10110111011111111101111100101101111011111111101111"
Returns: 336111964
"11111111111111101110111111111110110110011111111111"
Returns: 140154068
"0000011001010000000000000001000000010000000000000"
Returns: 338666891
"0000001000010001000000000000001010101001100000001"
Returns: 396043826
"0010101000000001000100010001000100000100110100000"
Returns: 786957552
"1111001000011101001100000011001001010011101100000"
Returns: 480715588
"0100010111000000001010111000010000111011001111010"
Returns: 290473414
"1100111011101001101100011111001110011100111001011"
Returns: 216398492
"1101111011101111111111111000111110111100110001011"
Returns: 802708929
"1111111001101111111111110101011111101101111111111"
Returns: 19623514
"1110111111111111111110111111110111101111101111111"
Returns: 6320868
"100001000001000010000000000001000000000000000100"
Returns: 286870100
"000000100001010000101010000010001000010100000000"
Returns: 769090031
"011011000100000100000101000100001010000010011101"
Returns: 784330660
"000100011100000101100100001011001010001000001010"
Returns: 296403103
"001000010110110010100000001101001001110111001011"
Returns: 298918426
"000100111110110110001011110010111010001001110111"
Returns: 879309899
"110111000111110111110001110111011111111111111111"
Returns: 358413900
"111111111011111011101111111110001111100111110111"
Returns: 449086028
"011111111111111111111111111111111111111111111111"
Returns: 31
"00010000000000000000000000100100000000000000000"
Returns: 492879102
"00000000000000001000001010000001100000100000000"
Returns: 695829908
"00000011001111000010100110010101101010101110101"
Returns: 525539449
"10111011111110111111100111111101111111101101111"
Returns: 742475379
"0000000000100001000000010000000000000100000000"
Returns: 193063553
"0011000000000000101000011000000000000000000000"
Returns: 315651978
"0111100001101001100101011100101101111001011110"
Returns: 30911112
"1100111111010111000111111010110101111001111110"
Returns: 239466636
"000000010000000000000000000000000000000000010"
Returns: 392288437
"100100000000000010000000100001000010000010000"
Returns: 381311126
"000001010000000010101011110001100110010011110"
Returns: 600028539
"111000101110111110110110010010111001011111100"
Returns: 840303746
"00000100000000000000000100000100001010000000"
Returns: 984138754
"00001000000100000010100010000010000011000010"
Returns: 739996136
"10000101110001101001110111000001000011010111"
Returns: 568681569
"11110111011111101111100111101001011111001111"
Returns: 297519725
"0000100000000010000001100000000000000000010"
Returns: 13265740
"0011000000010000100000100001000010100010100"
Returns: 77451083
"0100111101000010010011011111111001100000110"
Returns: 280692027
"1011110100111111111111111100111000100101111"
Returns: 191220100
"000000010100010000000000101000001100000010"
Returns: 809581057
"000000000000010100000001000011000000000000"
Returns: 846788726
"100110000010101000010101010011011011001001"
Returns: 667662895
"001001100110111000010010101011000110011101"
Returns: 420244527
"00010011010000000001000000000000010000000"
Returns: 389481453
"00100000010000000000000001000000000100010"
Returns: 895293971
"00110001011001110000001101110110111110101"
Returns: 626982611
"11111001111011111111101111111101111101011"
Returns: 205190457
"1101111"
Returns: 3
"0000100"
Returns: 48
"000000110100000"
Returns: 688262400
"111110111011111"
Returns: 56
"0100100001111011000110110110111"
Returns: 368188002
"1111101111011100101001110111111"
Returns: 730133466
"0101010101010101010101010101010101010101010101010"
Returns: 145599012
"10101010101010101010101010101010101010101010101"
Returns: 500212141
"001001001001001001001001001001001001001001001"
Returns: 558213385
"0110011001100110011001100110011001100110011001"
Returns: 575628658
"1011101110111011101110111011101110111011101110"
Returns: 368735950
"10100101001010010100101001010010100101001"
Returns: 106147781
"00110001100011000110001100011000110001100011000110"
Returns: 735386491
"00000000000000000000000000000000000000000000000000"
Returns: 0
"10000000000000000000000000000000000000000000000000"
Returns: 0
"10000000000000000000000000000000000000000000000001"
Returns: 0
"00000000000000000000000000000000000000000000000001"
Returns: 0
"0000000000000000000000000000000000000000000000110"
Returns: 0
"100000000000000000000000000000000000000000000110"
Returns: 0
"110000000000000000000000000000000000000000000110"
Returns: 457409939
"100000000000000000000000000000000000010"
Returns: 0
"11111111111111111111111111111110111111111111111111"
Returns: 22
"11111111111111111111011111111111111111111111111110"
Returns: 450
"11111101111111111111111111011111011111111111111111"
Returns: 14563
"10110111101111111111011111111111111111111111111111"
Returns: 570904
"01111111111111101111111111011111111111111111101011"
Returns: 6829712
"11111111111111111011111111111111101111111111111"
Returns: 520
"11111111111111111111101111111111111110011111111"
Returns: 8364