LeetCode 1590, Medium. Topics: Array, Hash Table, Prefix Sum. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
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
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^9
- 1 <= p <= 10^9
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026