max_length
to 0 to store the maximum length of subarray found so far.count
to 0 to keep track of the relative number of ones and zeros.count_map
with key as count
and value as the index at which this count
was first seen.count_map
with the entry (0, -1)
to handle the case when a valid subarray starts from index 0.count
by 1 for every 1
encountered and decrementing by 1 for every 0
.i
, check if count
is already in count_map
.
max_length
if this length is greater than the current max_length
.count
is not in count_map
, add it with the current index i
.max_length
.