Loading...
Loading...
Loading...
A log records 1 for a delivery and 0 for a return. Find the length of the longest consecutive stretch with the same number of deliveries and returns. Print 0 if there is none.
Example 1
Input
6 0 1 0 0 1 1
Output
6
The whole log is balanced.
Example 2
Input
3 1 1 1
Output
0
Try the problem first. Open one hint at a time.
Treat each 0 as -1. A balanced stretch then has total 0.
Two equal prefix totals mean the part between them adds to 0, so store the first position of each total.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor