InferenceAI

Enter at least two characters to search.

Start free

Balanced Binary Run

mediumArrays & Hashing25 min25 XP

bits contains only 0s and 1s. Return the length of the longest contiguous subarray with the same number of 0s and 1s.

Examples

EXAMPLE WALKTHROUGHpair

A guided view of the published example, not a trace of your code.

INPUT[[0,1]]
TASKbalanced_binary_run()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

Input: bits = [0,1]

Output: 2

Explanation: pair

Input: bits = [0,1,0]

Output: 2

Explanation: odd length

Input: bits = [0,0,1,0,0,0,1,1]

Output: 6

Explanation: longer

Hint 1

Treat 0 as -1. Then you want the longest subarray summing to 0.

Topics

DSAArrays & HashingPython