Short Java code, beats 100%
Approach\nCompute remainder of the whole sum, if it is 0 we are done. Otherwise, we need to compute prefix sum and traverse the array again: let\'s check if pre
# Approach\nCompute remainder of the whole sum, if it is 0 we are done. Otherwise, we need to compute prefix sum and traverse the array again: let\'s check if previously met prefix sum compliments the current prefix sum.\n\nThe last bit of optimization if the distance betwean complimentary indexes is 1 - do early return (that shaved run time from 22ms to 16ms). \n\n# Complexity\n- Time complexity: $$O(n ln(n))$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```java []\nclass Solution {\n    public int minSubarray(int[] nums, int p) {\n        long r = 0;\n        for (int i : nums) r += i;\n        r %= p;\n        if (r == 0) return 0;\n\n        int n = nums.length;\n        Map<Long, Integer> di = new HashMap(n+1);\n        di.put(0l, -1);\n\n        long[] ps = new long[n+1];\n        int mi = n;\n        for (int i = 0; i < n; i++) {\n            long rem = (ps[i] + nums[i]) % p;\n            ps[i+1] = rem;\n            long d = rem - r;\n            if (d < 0) d+= p;\n            if (di.containsKey(d)) {\n                mi = Math.min(mi, i - di.get(d));\n                if (mi == 1) return 1;\n            }\n\n            di.put(rem, i);\n        }\n        return mi == n ? -1 : mi;\n    }\n}\n```
['Java']