<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

static const bool Init = [](){
    std::ios_base::sync_with_stdio(false);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
    return true;
}();

inline bool is_digit(char c) {
    return c >= '0' && c <= '9';
}

std::vector<int> parse_input_string(const std::string& s) {
    std::vector<int> numbers;
    int num = -1;
    for (char c : s) {
        if (is_digit(c)) {
            if (num != -1) {
                num = num * 10 + (c - '0');
            } else {
                num = (c - '0');
            }
        } else {
            if (num != -1) {
                numbers.push_back(num);
            }
            num = -1;
        }
    }
    return numbers;
}

int findMaxLength(std::vector<int>& nums) {
    const int N = nums.size();
    std::vector<int> map(2 * N + 1, -2);
    map[N] = -1;

    int ans = 0;
    int count = 0;
    for(int i = 0; i < N; ++i) {
        count += (nums[i] == 1) ? 1 : -1;
        int idx = count + N;
        if(map[idx] == -2) {
            map[idx] = i;
        } else {
            ans = std::max(ans, i - map[idx]);
        }
    }
    return ans;
}

bool Solve = [](){
    std::ofstream out("user.out");
    for(std::string s; std::getline(std::cin, s);) {
        std::vector<int> nums_input = parse_input_string(s);
        out << findMaxLength(nums_input) << "\n";
    }
    out.flush();
    exit(0);
    return true;
}();
class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int n = nums.size();
        int pref = 0;
        int ans = INT_MIN;
        unordered_map<int, int> mpp;
        for (int i = 0; i < n; i++) {
            if (nums[i] == 0)
                pref--;
            if (nums[i] == 1)
                pref++;
            if (pref == 0)
                ans = max(ans, i + 1);
            if (mpp.find(pref) != mpp.end())
                ans = max(ans, i - mpp[pref]);
            else
                mpp[pref] = i;
        }
        return ans == INT_MIN ? 0 : ans;
    }
};
22000