C++ O(n)
There are many similar problems here; this is a simple one as we only track two states: even and odd number of negative elements.\n\nFor each state, we record t
There are many similar problems here; this is a simple one as we only track two states: even and odd number of negative elements.\n\nFor each state, we record the smallest index (`-1` for even to include the entire array), which we will use to calculate the maximum lenght.\n\nThere is just one catch: if we encounter `0`, we should start from the scratch.\n\n```cpp\nint getMaxLen(vector<int>& n, int start = 0) {\n    int cnt[] = {start - 1, INT_MAX}, max_len = 0, is_odd = false;\n    for (int i = start; i < n.size(); ++i) {\n        if (n[i] == 0)\n            return max(max_len, getMaxLen(n, i + 1));\n      is_odd = n[i] < 0 ? !is_odd : is_odd;\n      cnt[is_odd] = min(cnt[is_odd], i);\n      max_len = max(max_len, i - cnt[is_odd]);\n    }\n    return max_len;\n}\n```
[]