PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000141. Đoạn nhị phân cân bằng dài nhất (Contiguous Array)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đoạn nhị phân cân bằng dài nhất (Contiguous Array)
Contiguous Array
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given a binary array , call a contiguous interval balanced when it contains equally many zeros and ones. Find the maximum length of a nonempty balanced interval, or 0 if none exists.
Input
- Line 1: integer , the array length.
- Line 2: integers in order.
Output
Print the maximum balanced interval length, or 0 if none exists.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; ; for all .
- Subtask 3 (50 points): ; ; .
Example 1
Input
2
0 1
Output
2
Explanation
The whole array has one zero and one one, so the maximum length is 2.
Example 2
Input
3
0 1 0
Output
2
Explanation
Intervals and are balanced. The whole array has two zeros and one one, so length 3 is impossible.
Example 3
Input
9
0 1 1 1 1 1 0 0 0
Output
6
Explanation
Interval has three ones and three zeros. A balanced length is even; both length-8 intervals contain only three zeros, so 6 is maximal.