C++ Solution O(N)
Links which has helped a lot:\nhttps://leetcode.com/problems/make-sum-divisible-by-p/discuss/859173/DETAILED-EXPLANATION-OF-MATH-BEHIND-O(N)-SOLUTION\nhttps://l
Links which has helped a lot:\nhttps://leetcode.com/problems/make-sum-divisible-by-p/discuss/859173/DETAILED-EXPLANATION-OF-MATH-BEHIND-O(N)-SOLUTION\nhttps://leetcode.com/problems/make-sum-divisible-by-p/discuss/854174/C%2B%2BJava-O(n)\n\nRuntime: 360 ms, faster than 93.18% of C++ online submissions for Make Sum Divisible by P.\nMemory Usage: 66.8 MB, less than 89.47% of C++ online submissions for Make Sum Divisible by P.\n\n```\nA combination of Subarry Sum equal K and Subarray Sum Divisible by K problem.\n\nFirst we find the total sum of the array mod P => sumModP\n    If sumModP == 0  then total sum is divisible by P and so nothing to remove and return 0\n    Otherwise we need to find the shortest subarray which has sum%P == sumModP\n\nGo through the array and calculate the prefix sum and prefix sum mod P => prefixSumModP\n\tFind in prefixSumModP array if any sum equal sumModP which becomes "Subarray Sum equal K" problem\n    We need to find if complement of sumModP for prefixSumModP exists and count its subarray length\n    Store the recent index of prefixSumModP in HashMap which we will use to find length of subarray, Base case insert: (0,-1) in HashMap\n    So, the complement of sumModP, modComplement = (P + prefixSumModP - sumModP)%P\n        We add P cause prefixSumModP can be less than sumModP which makes (prefixSumModP - sumModP) negative\n        If modComplement exists then calculate length of the subarry as\n        current Index - index of modComeplement : i-prefixSumModTrack[modComplement] and find min length of subarray\n\nIf min length is size of array then removal is not possible and return -1\n        \nEx: [3, 1, 4, 2], P = 6\n    Total sum = 16, sumModP = 16%6 = 4\n    Initial HashMap = {0,-1}\n    Prefix Sum of given Array = [3, 4, 8, 10]\n    PrefixSumModP of given Array = [3, 4, 2, 4]\n    i = 0, (6+3-4)%6 = 5 => no compelement found => HashMap insert (3,0)\n    i = 1, (6+4-4)%6 = 0 => compelemnt found => minl = min(4,1-(-1)) = 2 => HashMap insert (4,1)\n    i = 2, (6+2-4)%6 = 4 => compelemnt found => minl = min(2,2-1) = 1 => HashMap insert (2,2)\n    i = 3, (6+4-4)%6 = 0 => compelemnt found => minl = min(1,3-(-1)) = 1 => HashMap insert (4,3)\n    So min Length of subarray sum%P = sumModP is 1 which is index 2.\n        \nMath proof of the complement of sumModP for given prefixSumModP:\n\nWe need to find subarray s[i:j] such that (Sum(s) - Sum(s(i:j)))%P == 0\nSo, Sum(s) - Sum(s(i:j)) = P.x (x is any positive value)\n=> Sum(s(i:j)) = Sum(s) - P.x\n=> Sum(s(0:i)) - Sum(s(0:j)) = Sum(s) - P.x\n=> (Sum(s(0:i)) - Sum(s(0:j)))%P = (Sum(s) - P.x)%P\n=> (Sum(s(0:i)) - Sum(s(0:j)))%P = Sum(s)%P\n=> (Sum(s(0:i)) - Sum(s(0:j)))%P = sumModP\n=> Sum(s(0:j))%P = (Sum(s(0:i))%P - sumModP)%P, this is the modCompelement equation \n\nSum(s(0:i))%P is the prefixSumModP and Sum(s(0:j))%P is modCompelemnt stored in HashMap \n\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        \n        int sumModP = 0;\n        \n        // Use modular arithmatic to avoid int overflow\n        for(int i=0;i<nums.size();i++)\n            sumModP = (sumModP + nums[i]) %p;\n        \n        // total sum mod P is reminder 0 and so nothing to remove and retuen 0\n        if(sumModP==0) return 0;\n        \n        unordered_map<int,int>prefixSumModTrack = {{0, -1}};\n        \n        int prefixSumModP  = 0;\n        int minLen = nums.size();\n        int modComplement;\n        for(int i=0;i<nums.size();i++)\n        {\n            // find mod of the prefix sum\n            prefixSumModP = (prefixSumModP + nums[i])%p;\n            // find complement of sumModP\n            modComplement = (p + prefixSumModP-sumModP)%p;\n            // If comeplement exsists then count the min length\n            if(prefixSumModTrack.count(modComplement))\n                minLen = min(minLen,i-prefixSumModTrack[modComplement]);\n            \n            // update current prefix sum mod with current index\n            prefixSumModTrack[prefixSumModP] = i;\n        }\n        \n        // if min length == array size then not possible\n        return minLen==nums.size()?-1:minLen;\n    }\n};\n```
['C']