File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,450 LeetCode solutions in JavaScript
1
+ # 1,451 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1425
1425
2799|[ Count Complete Subarrays in an Array] ( ./solutions/2799-count-complete-subarrays-in-an-array.js ) |Medium|
1426
1426
2818|[ Apply Operations to Maximize Score] ( ./solutions/2818-apply-operations-to-maximize-score.js ) |Hard|
1427
1427
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|
1428
1429
2873|[ Maximum Value of an Ordered Triplet I] ( ./solutions/2873-maximum-value-of-an-ordered-triplet-i.js ) |Easy|
1429
1430
2874|[ Maximum Value of an Ordered Triplet II] ( ./solutions/2874-maximum-value-of-an-ordered-triplet-ii.js ) |Medium|
1430
1431
2948|[ Make Lexicographically Smallest Array by Swapping Elements] ( ./solutions/2948-make-lexicographically-smallest-array-by-swapping-elements.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments