Detailed explanation, clear Python solution faster than 97.2%
Consider the example provided: nums = [6,3,5,2], p = 9\n\nWhat\'s the remainder when the total sum of the array elements is divided by \'p\' ? It\'s 7.\n\nIF it
Consider the example provided: **nums = [6,3,5,2], p = 9**\n\nWhat\'s the remainder when the *total sum of the array elements* is divided by \'p\' ? It\'s 7.\n\nIF it is indeed possible to remove a subarray, can\'t I say that **the total remainder, i.e. 7, is contributed by THAT subarray**? Because summing up the rest of the array and dividing the sum by \'p\' results in a remainder of 0.\n\nLet such a subarray be formed starting at index \'x\' and ending at index \'y\'. The remainder at index \'x\' is *rem1*, at \'y\' is *rem2*. So, if the difference between the remainders is equal to the **NET REMAINDER in the end**, we can say that removing this subarray satisfies our conditions for this problem. *(Again, think of it as removing the excess portion that\'s causing the remainder to be formed.)*\nIf \'curr\' is less than \'rem\', a minor change has to be made, as shown in the code.\n\nLet, **sum1 = the sum of array elements from index 0 to index \'x\' (BOTH INCLUSIVE)**\nSo, rem1 is really the remainder formed when sum1 is divided by \'p\'.\nSimilarly, let **sum2 = the sum of array elements from index 0 to index \'y\' (BOTH INCLUSIVE)**\nAgain, rem2 = remainder formed when sum2 is divided by \'p\'.\n\nFor the same example ( nums = [6,3,5,2], p = 9 ), let\'s look at the hash table at every iteration: [ quick reminder ; key = remainder, value = index ]\nAt iteration **1** (index 0) : **{ 0 : -1, 6 : 0 }**\nAt iteration **2** (index 1) : **{ 6 : 0, 0 : 1 }** ---> The value of key 0 has updated to reflect the latest position where the remainder of the running sum is 0\nAt iteration **3** (index 2) : **{ 6 : 0, 0 : 1, 5 : 2 }**\nAt iteration **4** (index 3) : **{ 6 : 0, 0 : 1, 5 : 2, 7 : 3 }**\n\nSo, if \'x\' is index 1 and \'y\' is index 3, rem1 and rem2 are **0** and **7** respectively.\n\nWe find that ( rem2 - rem1 ) is equal to the NET remainder. ( 7 )\n\nSo, I can safely say that :\n*If I remove the subarray starting at index (x + 1) and ending at index y, then it would leave me with only the array elements that give a remainder of 0, when their sum is divided by p.*\n\n```\nclass Solution:\n    def minSubarray(self, nums: List[int], p: int) -> int:\n        rem = sum(nums) % p\n        if rem == 0:\n            return 0\n        length = len(nums)\n        h = {0: -1}    # dictionary where \n                       # 1. key = remainder of running sum at each index, and\n                       # 2. value = index\n        run = [0]    # array to keep track of running sum\n        for i, num in enumerate(nums):\n            run.append(run[-1] + num)\n            curr = run[-1] % p\n            if curr >= rem:\n                if curr - rem in h:    # this portion of the array nums contributes to \'rem\'; i.e. gross remainder\n                    length = min(length, i - h[curr - rem])\n            else:\n                if p - rem + curr in h:    # again, this portion contributes to \'rem\'\n                    length = min(length, i - h[p - rem + curr])\n            h[curr] = i    # update every time to keep track of the latest occurrence of a particular remainder\n        if length == len(nums):\n            return -1\n        return length\n```
['Hash Table', 'Python', 'Python3']