Using HashMap.
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int minSubarray(int[] nums, int p) {\n        long psum=0;\n        for(int i=0;i<nums.length;i++){\n            psum+=nums[i];\n        }\n        int rem=(int)(psum%p);\n        if(rem==0){\n            return 0;\n        }\n        Map<Integer,Integer> map=new HashMap<>();\n        map.put(0,-1);\n        long sum=0;\n        int minl=nums.length;\n        for(int i=0;i<nums.length;++i){\n            sum+=nums[i];\n            int curmod=(int)(sum%p);\n            int tarmod=(curmod-rem+p)%p;\n            if(map.containsKey(tarmod)){\n                minl=Math.min(minl,i-map.get(tarmod));\n            }\n            map.put(curmod,i);\n        }\n        return minl==nums.length?-1:minl;\n    }\n}\n```
['Java']