-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path2261-k-divisible-elements-subarrays.js
More file actions
41 lines (36 loc) · 1.05 KB
/
2261-k-divisible-elements-subarrays.js
File metadata and controls
41 lines (36 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 2261. K Divisible Elements Subarrays
* https://leetcode.com/problems/k-divisible-elements-subarrays/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} nums
* @param {number} k
* @param {number} p
* @return {number}
*/
var countDistinct = function(nums, k, p) {
const n = nums.length;
const seen = new Set();
for (let start = 0; start < n; start++) {
const subarray = [];
let divisibleCount = 0;
for (let end = start; end < n; end++) {
subarray.push(nums[end]);
if (nums[end] % p === 0) divisibleCount++;
if (divisibleCount <= k) {
seen.add(subarray.join(','));
}
}
}
return seen.size;
};