Efficient Approach||Using C++✅✅
Intuition\nThe problem asks for the smallest subarray that can be removed so that the sum of the remaining array is divisible by p. By calculating the total sum
**Intuition**\nThe problem asks for the smallest subarray that can be removed so that the sum of the remaining array is divisible by p. By calculating the total sum and its remainder when divided by p, we know what subarray needs to be removed (its sum modulo p should match this remainder). The idea is to efficiently find this subarray using prefix sums and a hash map.\n\n**Approach**\nTotal Sum Calculation: First, compute the total sum of the array. If the total sum is divisible by p, we return 0 since no subarray needs to be removed.\n\nPrefix Sum and Hash Map: We use a prefix sum to keep track of the cumulative sum as we traverse the array. For each element, we calculate the prefix sum modulo p and store it in a hash map, with the key being the remainder and the value being the index.\n\nEfficient Subarray Identification: For each index i, we calculate the target mod value ((prefixSum - remainder) % p). If this mod value has been seen before, we compute the length of the subarray that can be removed, and update the minimum length.\n\nEdge Cases: If no valid subarray is found, return -1. Otherwise, return the minimum length of the subarray.\n\n**Time Complexity**\nO(n): We traverse the array once to calculate the total sum and then traverse it again to compute prefix sums and use the hash map for efficient lookups. All operations (prefix sum, mod operations, and hash map lookups) are performed in constant time, so the overall time complexity is linear with respect to the size of the array.\n**Space Complexity**\nO(n): The hash map stores the mod values of the prefix sums, which in the worst case requires space proportional to the size of the input array. Thus, the space complexity is linear.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        long long totalSum = 0;\n        int n = nums.size();\n\n        // Calculate the total sum of the array\n        for (int i = 0; i < n; i++) {\n            totalSum += nums[i];\n        }\n\n        // Find the remainder when totalSum is divided by p\n        int remainder = totalSum % p;\n        if (remainder == 0) {\n            return 0; // The sum is already divisible by p, no subarray needs to be removed\n        }\n\n        // Hash map to store the mod value of prefix sums\n        unordered_map<int, int> prefixModIndex;\n        prefixModIndex[0] = -1; // Handle the case where the subarray starts from the beginning\n        long long prefixSum = 0;\n        int minLength = n;\n\n        // Traverse the array and calculate prefix sums\n        for (int i = 0; i < n; i++) {\n            prefixSum = (prefixSum + nums[i]) % p;\n\n            // Find the target mod value that would make the remaining array sum divisible by p\n            int targetMod = (prefixSum - remainder + p) % p;\n\n            // If this targetMod was seen before, calculate the length of the subarray\n            if (prefixModIndex.find(targetMod) != prefixModIndex.end()) {\n                minLength = min(minLength, i - prefixModIndex[targetMod]);\n            }\n\n            // Store the current prefixSum mod p with its index\n            prefixModIndex[prefixSum] = i;\n        }\n\n        // If no valid subarray is found, return -1, otherwise return the minimum length\n        return (minLength == n) ? -1 : minLength;\n    }\n};\n\n```
['C++']