Interview like explanation, understand once with added video links
Youtube Explanation https://youtu.be/1hjPA6YbulE\n\n# Intuition and Approach\nLets keep things simple :- \n\nfirst of all we have to find the sum of all numbers
# Youtube Explanation https://youtu.be/1hjPA6YbulE\n\n# Intuition and Approach\nLets keep things simple :- \n\nfirst of all we have to find the sum of all numbers and its mod with P so that we will come to know what value we have to remove in the form of a sub array.\n for(int x : nums)\n    **sum = (sum + x)%p;**\n   ** mod = sum%p;**\n\n**But bro, why are you doing like this? first get sum of all numbers and then use Mod with p, why in for loop for all values?**\n\ngreat question, we can do that but bro, think if the numbers in the array are huge like [100000000000, 10000000000, 100000000000] and if you go for sum first => then this will lead to overflow as Integer can not park that huge value after sum...so lets make use of Modulous.\n\n**ok, got it, next?**\nnext we will store the prefixMod of all the sum starting from 0 index in the given array in our HashMap.\n\n**why? what is the reason behind this??**\nok lets undersatnd it in simple way\n\n**forget about Mod sum for a while**, just think we have only prefix sum stored in hashmap now at any point i lets say we have **sum = SUMi** , and suppose there is another sum at index j already = SUMj\n\nthen **sum between i and j index SUM-ij =  (SUMi - SUMj)**\nnow if we find that this (SUM-ij)%p = mod (we calculated in start)\nthat means we found at least one sub-array residing at **(i till j index)**\n\nso we can write at any index if our condition is mathing like this \n\n(SUMi - SUMj)%p = **mod** ( mod calculated in start)\n\n=> **SUMj%p** = (**SUMi%p**- mod + p)%p\n=> so the above two bold values are actually the prefixMod\n\n**BUT BUT bro why you added p?**\nbecause we don\'t want the negative remainder values that\'s why.\n\n**can you gimme an example?**\nsure, lets say, our mod = 7, p = 8 and at index i we found prefisSum mod = 3 in that case it would give us 3 -7 = -4 but if add p it will give us 3 - 7 + 8 = 4\n\n**ok so next??**\nso next is we are looking for prefixMod if already occured, like in above case if any prefix mod = 4 at index j, already recorded in hasmap, that means we got one answer of length (i - j)\nif we don\'t find such value we have to store current prefixSumMod and index i in hashMap.\n\n**but we need smallest one, right?**\nexactly, so we need to traverse through array and would look for min value till end.\n\n**so finally our min value would be answer??**\n\nnot always bro, first of all if our mod = 0 => means array sum is already divisible so we return 0 itself in start.\n\nother case is we initialized our ans = n (length of array)\nnow we were looking for min value\n\n**ok so what??**\n\nso if did\'t find any value in entire array our ans would still have untouched value ans = n ===> that means no value can satify our condition so return -1 other wise return ans\n\nreturn ans==n ? -1 : ans.\n\n**still have issue?, no problem soon would release Youtube video**\n\n\n# Code\n```java []\nclass Solution {\n    public int minSubarray(int[] nums, int p) {\n    int sum = 0;    \n    for(int x : nums)\n    sum = (sum + x)%p;\n  \n    int mod = sum%p; \n    if(mod==0)\n    return 0;\n\n    int ans = nums.length;\n    HashMap<Integer, Integer>hm = new HashMap<>();\n    hm.put(0, -1);\n    int currSumMod = 0;\n    for(int i=0; i<nums.length; i++)\n    {\n      currSumMod = (currSumMod+nums[i])%p;\n      int neededMod = (currSumMod - mod + p)%p;\n      if(hm.containsKey(neededMod))\n      ans = Math.min(ans, i-hm.get(neededMod));\n      hm.put(currSumMod, i);\n    }\n     return ans==nums.length ? -1 : ans;\n    }\n}\n```
['Array', 'Hash Table', 'Math', 'Ordered Map', 'Hash Function', 'Java']