Find maximum length sub-array having equal number of 0's and 1's

Given an binary array containing 0's and 1's, find the maximum length sub-array having equal number of 0's and 1's. Return the first sub-array.

Example Solution

When the solution is called with the following input arr = [0, 0, 1, 0, 1, 0, 0] it is expected to return the array [0, 1, 0, 1].

Starting from the index 1, we can observe the sequence of numbers 0, 1, 0 and 1. These integers create the longest sub-array. There's also another sub-array with the same length starting from index 2: 1, 0, 1 and 0 but the first sub-array is the correct answer since it's begin index is closer to 0.

Contributed by Murat Sütunç