Typescript Approach [O(n) Time/Space]
Intuition\nTo solve this problem, the goal is to remove the smallest subarray such that the sum of the remaining elements is divisible by p. This means we need 
# Intuition\nTo solve this problem, the goal is to remove the smallest subarray such that the sum of the remaining elements is divisible by `p`. This means we need to focus on the sum of the array and how it relates to `p`.\n\n# Approach\n1. Key Insight:\n\n- The sum of the remaining elements should be divisible by `p`. Therefore, the sum of the entire array `sum(nums)` modulo `p` should equal the sum of the removed subarray modulo `p`.\n- Specifically, let `total_sum` be the sum of all elements in `nums`. We need to find a subarray whose sum, when removed, makes the remaining sum divisible by `p`.\n- If `total_sum % p == 0`, there\'s no need to remove any subarray because the entire sum is already divisible by `p`. In this case, return 0.\n- Otherwise, we need to find a subarray whose sum modulo `p` equals `total_sum % p`.\n\n2. Using Prefix Sum:\n\n- We\'ll use a prefix sum approach to efficiently compute the sum of subarrays.\n- We can calculate the prefix sum modulo `p` for every index. The key idea is to look for two indices `i` and `j` (with `i < j`) such that the difference between their prefix sums modulo `p` is equal to `total_sum % p`. This indicates that the subarray between indices `i+1` and `j` has a sum that will make the total sum divisible by `p`.\n3. Optimize with a HashMap: To optimize the search for such subarrays, we can maintain a hash map that stores the latest index for each prefix sum modulo `p` encountered so far. This helps us efficiently find the smallest subarray that meets the condition.\n4. Algorithm:\n\n- First, compute the total sum modulo `p`.\n- Use a running prefix sum modulo `p` to find the smallest subarray whose sum is `total_sum % p`.\n- Use a hash map to store the indices of the prefix sums modulo `p` as we iterate through the array.\n\n# Complexity\n- Time complexity:\nWe iterate through the array once to compute the prefix sums and check conditions, making this approach $O(n)$, where n is the length of the array.\n\n- Space complexity:\nWe use a hash map to store the prefix sums, which takes $O(n)$ space.\n\n# Code\n```typescript []\nfunction minSubarray(nums: number[], p: number): number {\n    const totalSum = nums.reduce((sum, num) => sum + num, 0)\n    const target = totalSum % p\n    \n    if (target === 0) return 0\n\n    const prefixSumMap = new Map<number, number>()\n    prefixSumMap.set(0, -1)\n\n    let prefixSum = 0\n    let minLength = nums.length\n\n    for (let i = 0; i < nums.length; i++) {\n        prefixSum = (prefixSum + nums[i]) % p\n\n        const neededPrefix = (prefixSum - target + p) % p\n\n        if (prefixSumMap.has(neededPrefix)) {\n            minLength = Math.min(minLength, i - prefixSumMap.get(neededPrefix)!)\n        }\n\n        prefixSumMap.set(prefixSum, i)\n    }\n\n    return minLength === nums.length ? -1 : minLength\n};\n```
['Hash Table', 'TypeScript', 'JavaScript']