[Java/C++/Python] One Pass, Count Zero Score
Intuition\nBecause (x & y) <= x and (x & y) <= y,\nso the score(array) <= score(subarray).\n\nIf the score of A is x,\nthen the score of each subarray of A bigg
# **Intuition**\nBecause `(x & y) <= x` and `(x & y) <= y`,\nso the `score(array) <= score(subarray)`.\n\nIf the score of `A` is `x`,\nthen the score of each subarray of `A` bigger or equal to `x`.\n\nIf `x > 0`,\n`2x > x`,\nso we won\'t split `A`,\nreturn 1.\n\nIf `x == 0`,\nso we may split `A`,\ninto multiple subarray with score `0`.\n\nSo the real problem is,\nsplit `A` into as many subarray as possbile,\nwith socre `0`\n<br>\n\n# **Explanation**\nCalculate the prefix score of `A`,\nif score is `0`,\nwe can split out the prefix as a subarray with socre `0`,\nthen continue the above process.\n<br>\n\n# Tips 1: -1\nWe initialize the prefix score with `-1`,\nwhose all bits are `1`,\nmaximum integer also works.\n<br>\n\n# Tips 2: no zero score\nIn case that score of `A` is positive,\nthere is no `0` score array,\nstill at least one array of `A`,\nso we return `max(1, res)`.\n<br>\n\n# Tips 3: Greedy\nI have greedily split the subarray with score `0`.\nUsually people like a prove all all greedy ideas,\nI leave for you to have a try.\n<br>\n\n# **Complexity**\nTime `O(n)`\nSpace `O(1)`\n<br>\n\n**Java**\n```java\n    public int maxSubarrays(int[] A) {\n        int v = -1, res = 0;\n        for (int a: A) {\n            v &= a;\n            if (v == 0) {\n                v = -1;\n                res += 1;\n            }\n        }\n        return Math.max(1, res);\n    }\n```\n\n**C++**\n```cpp\n    int maxSubarrays(vector<int>& A) {\n        int v = -1, res = 0;\n        for (int a: A)\n            if ((v &= a) == 0)\n                v--, res++;\n        return max(1, res);\n    }\n```\n\n**Python**\n```py\n    def maxSubarrays(self, A: List[int]) -> int:\n        v = -1\n        res = 0\n        for a in A:\n            v &= a\n            if v == 0:\n                v = -1\n                res += 1\n        return max(1, res)\n```\n
['C', 'Python', 'Java']