> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Make Sum Divisible by P Python Solution

> Tested Python solution for LeetCode 1590 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1590, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/make-sum-divisible-by-p/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1590   # by problem number
lcpy gen -s make_sum_divisible_by_p   # by problem name
```

## Problem

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's impossible\</em>.\</p>

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

### Examples

```
Input: nums = [3,1,4,2], p = 6
Output: 1
Explanation: 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.
```

```
Input: nums = [6,3,5,2], p = 9
Output: 2
Explanation: 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.
```

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

### Constraints

* 1 \<= nums.length \<= 10^5
* 1 \<= nums\[i] \<= 10^9
* 1 \<= p \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/make_sum_divisible_by_p/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/make_sum_divisible_by_p/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(min(n, p))
    def min_subarray(self, nums: list[int], p: int) -> int:
        total = sum(nums) % p
        if total == 0:
            return 0
        n = len(nums)
        last = {0: -1}
        cur = 0
        best = n
        for i, num in enumerate(nums):
            cur = (cur + num) % p
            need = (cur - total) % p
            if need in last:
                best = min(best, i - last[need])
            last[cur] = i
        return -1 if best == n else best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(min(n, p)) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.