Question DSA-HASH-0062
HardWhat is the output of this code?
def longest(a):
first, s, best = {0: -1}, 0, 0
for i, x in enumerate(a):
s += 1 if x == 1 else -1
if s in first:
best = max(best, i - first[s])
else:
first[s] = i
return best
print(longest([0, 1, 0, 0, 1, 1, 0]))
- A.
4
- B.
7
- C.
Sign up free to see the answer and a step-by-step explanation.