Skip to content

Practice Make Sum Divisible by P

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1590
LeetCode Problem

Make Sum Divisible by P

Problem Statement

Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array. Return the length of the smallest subarray that you need to remove, or -1 if it's impossible. A subarray is defined as a contiguous block of elements in the array.

Source: Make Sum Divisible by P on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [3,1,4,2], p = 6
Output
1

# Example 2

Input
nums = [6,3,5,2], p = 9
Output
2

# Example 3

Input
nums = [1,2,3], p = 3
Output
0

Constraints

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

Related LeetCode Problems

Practice more problems similar to Make Sum Divisible by P.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.