Skip to content

Practice K Divisible Elements Subarrays

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

Medium#2261
LeetCode Problem

K Divisible Elements Subarrays

Problem Statement

Count distinct nonempty contiguous subarrays of nums containing at most k values divisible by p. Two subarrays are the same when their lengths and values at every position match, even if they occur at different original positions. Follow-up: can you achieve O(n^2) time?

Source: K Divisible Elements Subarrays on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,3,3,2,2], k = 2, p = 2
Output
11

# Example 2

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

Constraints

1 <= nums.length <= 200
1 <= nums[i], p <= 200
1 <= k <= nums.length

Related LeetCode Problems

Practice more problems similar to K Divisible Elements Subarrays.

Review your practice with PhantomCodeAI

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