[Java/C++/Python] Sliding Window, O(1) Space
Solution 1: atMost\nHave you read this? 992. Subarrays with K Different Integers\nExactly K times = at most K times - at most K - 1 times\n\n\n# Complexity\nTim
# **Solution 1: atMost**\nHave you read this? [992. Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/523136/JavaC%2B%2BPython-Sliding-Window)\nExactly `K` times = at most `K` times - at most `K - 1` times\n<br>\n\n# **Complexity**\nTime `O(N)` for one pass\nSpace `O(1)`\n<br>\n\n**Java:**\n```java\n    public int numberOfSubarrays(int[] A, int k) {\n        return atMost(A, k) - atMost(A, k - 1);\n    }\n\n    public int atMost(int[] A, int k) {\n        int res = 0, i = 0, n = A.length;\n        for (int j = 0; j < n; j++) {\n            k -= A[j] % 2;\n            while (k < 0)\n                k += A[i++] % 2;\n            res += j - i + 1;\n        }\n        return res;\n    }\n```\n\n**C++:**\n```cpp\n    int numberOfSubarrays(vector<int>& A, int k) {\n        return atMost(A, k) - atMost(A, k - 1);\n    }\n\n    int atMost(vector<int>& A, int k) {\n        int res = 0, i = 0, n = A.size();\n        for (int j = 0; j < n; j++) {\n            k -= A[j] % 2;\n            while (k < 0)\n                k += A[i++] % 2;\n            res += j - i + 1;\n        }\n        return res;\n    }\n```\n\n**Python:**\n```python\n    def numberOfSubarrays(self, A, k):\n        def atMost(k):\n            res = i = 0\n            for j in xrange(len(A)):\n                k -= A[j] % 2\n                while k < 0:\n                    k += A[i] % 2\n                    i += 1\n                res += j - i + 1\n            return res\n\n        return atMost(k) - atMost(k - 1)\n```\n<br><br>\n\n# Solution II: One pass\n\nActually it\'s same as three pointers.\nThough we use `count` to count the number of even numebers.\nInsprired by @yannlecun.\n\nTime `O(N)` for one pass\nSpace `O(1)`\n<br>\n\n**Java:**\n```java\n    public int numberOfSubarrays(int[] A, int k) {\n        int res = 0, i = 0, count = 0, n = A.length;\n        for (int j = 0; j < n; j++) {\n            if (A[j] % 2 == 1) {\n                --k;\n                count = 0;\n            }\n            while (k == 0) {\n                k += A[i++] & 1;\n                ++count;\n            }\n            res += count;\n        }\n        return res;\n    }\n```\n\n**C++:**\n```cpp\n    int numberOfSubarrays(vector<int>& A, int k) {\n        int res = 0, i = 0, count = 0, n = A.size();\n        for (int j = 0; j < n; j++) {\n            if (A[j] & 1)\n                --k, count = 0;\n            while (k == 0)\n                k += A[i++] & 1, ++count;\n            res += count;\n        }\n        return res;\n    }\n```\n\n**Python:**\n```python\n    def numberOfSubarrays(self, A, k):\n        i = count = res = 0\n        for j in xrange(len(A)):\n            if A[j] & 1:\n                k -= 1\n                count = 0\n            while k == 0:\n                k += A[i] & 1\n                i += 1\n                count += 1\n            res += count\n        return res\n```\n\n# More Similar Sliding Window Problems\nHere are some similar sliding window problems.\nAlso find more explanations.\nGood luck and have fun.\n\n\n- 1358. [Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/discuss/516977/JavaC++Python-Easy-and-Concise)\n- 1248. [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/discuss/419378/JavaC%2B%2BPython-Sliding-Window-atMost(K)-atMost(K-1))\n- 1234. [Replace the Substring for Balanced String](https://leetcode.com/problems/replace-the-substring-for-balanced-string/discuss/408978/javacpython-sliding-window/367697)\n- 1004. [Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/discuss/247564/javacpython-sliding-window/379427?page=3)\n-  930. [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/discuss/186683/)\n-  992. [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/523136/JavaC%2B%2BPython-Sliding-Window)\n-  904. [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/discuss/170740/Sliding-Window-for-K-Elements)\n-  862. [Shortest Subarray with Sum at Least K](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/discuss/143726/C%2B%2BJavaPython-O(N)-Using-Deque)\n-  209. [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/discuss/433123/JavaC++Python-Sliding-Window)\n<br>
[]