[Java/Python 3] O(n) code w/ brief explanation, analysis and similar problems.
Similar problems:\n\n1. Two Sum\n325. Maximum Size Subarray Sum Equals k: Premium \n525. Contiguous Array\n560. Subarray Sum Equals K\n974. Subarray Sums Divisi
Similar problems:\n\n[1. Two Sum](https://leetcode.com/problems/two-sum/description/)\n[325. Maximum Size Subarray Sum Equals k: Premium](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k) \n[525. Contiguous Array](https://leetcode.com/problems/contiguous-array)\n[560. Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/description/)\n[974. Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/description/)\n[1010. Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/discuss/256726/JavaPython-3-O(n)-code-w-comment-similar-to-Two-Sum)\n[1497. Check If Array Pairs Are Divisible by k](https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/discuss/709226/JavaPython-3-Count-positive-remainder-and-cancel-the-other-whenever-locate-a-pair.)\n\n----\n1. Find the remainder of the `sum(nums) % p`;\n2. Use a HashMap to store the `(prefix-sum % p == last index)` bindings;\n3. Traverse the input array to compute the prefix sum and seach its congruence in the afore-mentioned HashMap; Update the minimum length of the subarray if found;\n4. If the minimum length is still the initial value after traversal, return `-1`; otherwise, return the minimun.\n```java\n    public int minSubarray(int[] nums, int p) {\n        int remainder = 0, prefixSum = 0; \n        int n = nums.length, minLen = n;\n        for (int num : nums) {\n            remainder = (remainder + num) % p; \n        }\n        var prefixSumToIndex = new HashMap<Integer, Integer>();\n        prefixSumToIndex.put(prefixSum, -1);\n        for (int i = 0; i < n; ++i) {\n            prefixSum = (prefixSum + nums[i]) % p;\n            prefixSumToIndex.put(prefixSum, i);\n            int key = (prefixSum - remainder + p) % p;\n            if (prefixSumToIndex.containsKey(key)) {\n                minLen = Math.min(minLen, i - prefixSumToIndex.get(key));\n            }\n        }\n        return minLen == n ? -1 : minLen;        \n    }\n```\n```python\n    def minSubarray(self, nums: List[int], p: int) -> int:\n        remainder, min_len, prefix_sum = sum(nums) % p, len(nums), 0\n        prefix_sum_to_index = {prefix_sum : -1}\n        for i, num in enumerate(nums):\n            prefix_sum = (prefix_sum + num) % p\n            key = (prefix_sum - remainder) % p\n            prefix_sum_to_index[prefix_sum] = i\n            if key in prefix_sum_to_index:\n                min_len = min(min_len, i - prefix_sum_to_index[key])\n        return min_len if min_len < len(nums) else -1   \n```\n**Analysis:**\nTime & space: O(n).
[]