Sliding Window, Hash Map approach
Intuition\n Describe your first thoughts on how to solve this problem. \nWhenever I read the word "Subsequence", I blindly apply sliding window.\n\n# Approach\n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhenever I read the word "Subsequence", I blindly apply sliding window.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo find the smallest subarray whose removal makes the sum of the remaining array divisible by `p`, I first calculate the total sum and its remainder `rem = sum % p`. If `rem == 0`, I return 0 since the sum is already divisible by `p`. \n\nUsing a sliding window approach, I maintain the running remainder of the prefix sums. By tracking these remainders in a hashmap, I efficiently check for subarrays whose removal achieves the desired result, updating the minimum subarray length when found.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        int n = nums.size();\n        int totalSum = 0;\n    \n    \n        for (int num : nums) {\n            totalSum = (totalSum + num) % p;\n        }\n    \n        if (totalSum == 0) {\n            return 0;\n        }\n    \n    \n        int remainderToRemove = totalSum;\n    \n    \n        unordered_map<int, int> prefixMod;\n        prefixMod[0] = -1;  \n        int prefixSum = 0;\n        int minLen = n;\n    \n   \n        for (int i = 0; i < n; ++i) {\n            prefixSum = (prefixSum + nums[i]) % p;\n            int target = (prefixSum - remainderToRemove + p) % p;\n        \n            if (prefixMod.find(target) != prefixMod.end()) {\n                minLen = min(minLen, i - prefixMod[target]);\n            }\n        \n        \n            prefixMod[prefixSum] = i;\n        }\n    \n    \n        return (minLen == n) ? -1 : minLen;\n    }\n\n\n};\n```
['C++']