|
| 1 | +<h2><a href="https://leetcode.com/problems/make-sum-divisible-by-p">1694. Make Sum Divisible by P</a></h2><h3>Medium</h3><hr><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> |
| 2 | + |
| 3 | +<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> |
| 4 | + |
| 5 | +<p>A <strong>subarray</strong> is defined as a contiguous block of elements in the array.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> nums = [3,1,4,2], p = 6 |
| 12 | +<strong>Output:</strong> 1 |
| 13 | +<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. |
| 14 | +</pre> |
| 15 | + |
| 16 | +<p><strong class="example">Example 2:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> nums = [6,3,5,2], p = 9 |
| 20 | +<strong>Output:</strong> 2 |
| 21 | +<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. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 3:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> nums = [1,2,3], p = 3 |
| 28 | +<strong>Output:</strong> 0 |
| 29 | +<strong>Explanation:</strong> Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 37 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 38 | + <li><code>1 <= p <= 10<sup>9</sup></code></li> |
| 39 | +</ul> |
0 commit comments