K Divisible Elements Subarrays

Given an integer array nums and two integers k and p, return the number of distinct subarrays which have at most k elements that are divisible by p.

Two arrays nums1 and nums2 are said to be distinct if:

  • They are of different lengths, or
  • There exists at least one index i where nums1[i] != nums2[i].

A subarray is defined as a non-empty contiguous sequence of elements in an array.

Follow up: Can you solve this problem in O(n^2) time complexity?

Example 1
Inputnums = [2,3,3,2,2], k = 2, p = 2
Output11
The 11 listed distinct subarrays have at most 2 elements divisible by 2, while [2,3,3,2,2] has 3 such elements and is not counted.
Example 2
Inputnums = [1,2,3,4], k = 4, p = 1
Output10
Every subarray has at most 4 elements divisible by 1, and all 10 subarrays of nums are distinct.

Constraints

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

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate