Skip to content

Commit af9e612

Browse files
committed
Add solution #2845
1 parent 84eacb7 commit af9e612

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,450 LeetCode solutions in JavaScript
1+
# 1,451 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1425,6 +1425,7 @@
14251425
2799|[Count Complete Subarrays in an Array](./solutions/2799-count-complete-subarrays-in-an-array.js)|Medium|
14261426
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
14271427
2843|[Count Symmetric Integers](./solutions/2843-count-symmetric-integers.js)|Easy|
1428+
2845|[Count of Interesting Subarrays](./solutions/2845-count-of-interesting-subarrays.js)|Medium|
14281429
2873|[Maximum Value of an Ordered Triplet I](./solutions/2873-maximum-value-of-an-ordered-triplet-i.js)|Easy|
14291430
2874|[Maximum Value of an Ordered Triplet II](./solutions/2874-maximum-value-of-an-ordered-triplet-ii.js)|Medium|
14301431
2948|[Make Lexicographically Smallest Array by Swapping Elements](./solutions/2948-make-lexicographically-smallest-array-by-swapping-elements.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 2845. Count of Interesting Subarrays
3+
* https://leetcode.com/problems/count-of-interesting-subarrays/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums, an integer modulo, and an integer k.
7+
*
8+
* Your task is to find the count of subarrays that are interesting.
9+
*
10+
* A subarray nums[l..r] is interesting if the following condition holds:
11+
* - Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k.
12+
* Then, cnt % modulo == k.
13+
*
14+
* Return an integer denoting the count of interesting subarrays.
15+
*
16+
* Note: A subarray is a contiguous non-empty sequence of elements within an array.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @param {number} modulo
22+
* @param {number} k
23+
* @return {number}
24+
*/
25+
var countInterestingSubarrays = function(nums, modulo, k) {
26+
const prefixCounts = new Map([[0, 1]]);
27+
let currentSum = 0;
28+
let result = 0;
29+
30+
for (const num of nums) {
31+
currentSum = (currentSum + (num % modulo === k ? 1 : 0)) % modulo;
32+
result += prefixCounts.get((currentSum - k + modulo) % modulo) || 0;
33+
prefixCounts.set(currentSum, (prefixCounts.get(currentSum) || 0) + 1);
34+
}
35+
36+
return result;
37+
};

0 commit comments

Comments
 (0)