<p>Given an array of positive integers <code>nums</code>, remove the <strong>smallest</strong> subarray (possibly <strong>empty</strong>) such that the <strong>sum</strong> of the remaining elements is divisible by <code>p</code>. It is <strong>not</strong> allowed to remove the whole array.</p>

<p>Return <em>the length of the smallest subarray that you need to remove, or </em><code>-1</code><em> if it&#39;s impossible</em>.</p>

<p>A <strong>subarray</strong> is defined as a contiguous block of elements in the array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,4,2], p = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> The sum of the elements in nums is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [6,3,5,2], p = 9
<strong>Output:</strong> 2
<strong>Explanation:</strong> We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5,2], leaving us with [6,3] with sum 9.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3], p = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= p &lt;= 10<sup>9</sup></code></li>
</ul>

from typing import List

class Solution:
    def minSubarray(self, nums: List[int], p: int) -> int:
        # Calculate the total sum and the target remainder
        total_sum = sum(nums)
        target_remainder = total_sum % p
        
        # If the total sum is already divisible by p, no need to remove any subarray
        if target_remainder == 0:
            return 0
        
        # Dictionary to store the prefix sum modulo p and the index where it was last seen
        prefix_sum_mod_p = {0: -1}
        current_sum = 0
        min_length = len(nums)
        
        for i, num in enumerate(nums):
            # Update the current sum and its modulo p
            current_sum += num
            current_sum_mod_p = current_sum % p
            
            # Calculate the remainder we need to find in the prefix sums
            needed_remainder = (current_sum_mod_p - target_remainder) % p
            
            # If the needed remainder is in the dictionary, update the minimum length
            if needed_remainder in prefix_sum_mod_p:
                length = i - prefix_sum_mod_p[needed_remainder]
                min_length = min(min_length, length)
            
            # Update the dictionary with the current sum modulo p
            prefix_sum_mod_p[current_sum_mod_p] = i
        
        # If the minimum length is equal to the length of the array, it means we cannot remove any subarray to achieve the target
        return min_length if min_length < len(nums) else -1

# Example usage:
solution = Solution()
nums = [3,1,4,2]
p = 6
print(solution.minSubarray(nums, p))  # Output: 1

nums = [6,3,5,2]
p = 9
print(solution.minSubarray(nums, p))  # Output: 2

nums = [1,2,3]
p = 3
print(solution.minSubarray(nums, p))  # Output: 0
394