Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Count the binary strings of length 10 that contain no two adjacent 1s and also contain a run of at least three consecutive 0s. Brute-force all 1024 strings.
Count the binary strings of length 10 that contain no two adjacent 1s and also contain a run of at least three consecutive 0s. Brute-force all 1024 strings.
import itertools
c=0
for t in itertools.product('01',repeat=10):
s=''.join(t)
if '11' not in s and '000' in s: c+=1
print(c)What is printed?- A. 75
- B. 144
- C. 142
- D. 116
Correct answer: D. 116
Filter strings by the no-11 rule and require a 000 block, then count. Dropping the run requirement counts all no-11 strings, requiring only a pair of zeros admits more strings, and demanding four zeros excludes valid ones.
More Coding Interview Problems questions
- Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code. python print(sum(1 for a in range(1,21) for…
- Floyd–Warshall is run on the 4-vertex directed weighted graph with matrix rows [0,3,∞,7], [8,0,2,∞], [5,∞,0,1], [2,∞,∞,0]. Sum the final…
- A rod of length 8 can be cut into integer pieces, and a piece of length L sells for p[L] with p = [1, 5, 8, 9, 10, 17, 17, 20] for L =…
- Array b = [1, 11, 2, 10, 4, 5, 2, 1]. Find the length of its longest bitonic subsequence: a subsequence that strictly increases and then…
- Count the 4-element subsets of {1, 2, ..., 12} whose sum is divisible by 5. Count by brute force over combinations. python from itertools…
- Find the smallest positive integer x such that x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7). Solve by brute-force search. python…