Java O(n) solution
\nclass Solution {\n    public int minSubarray(int[] a, int p) {\n\n        long temp = 0;\n        for(int i=0;i<a.length;i++) temp+=a[i];\n        \n        i
```\nclass Solution {\n    public int minSubarray(int[] a, int p) {\n\n        long temp = 0;\n        for(int i=0;i<a.length;i++) temp+=a[i];\n        \n        if(temp%p == 0) return 0;\n        \n        int rem = (int)(temp%p);\n                 \n        HashMap<Integer,Integer> map = new HashMap();\n        map.put(0,-1);\n        int size = a.length;\n          temp= 0;\n\n        for(int i=0;i<a.length;i++){\n            temp = (temp+a[i]);\n\n                  int newVar = (int)((temp-rem)%p);\n                 if(map.containsKey(newVar)){\n                   size = Math.min(i-map.get(newVar),size);\n                 }  \n                  map.put( (int)(temp%p),i);\n        }\n        \n        return (size == a.length) ? -1 : size;\n    }\n}\n```
[]