diff --git a/leetcode/daily/2025-04-25-count-of-interesting-subarrays.js b/leetcode/daily/2025-04-25-count-of-interesting-subarrays.js new file mode 100644 index 0000000..bf02549 --- /dev/null +++ b/leetcode/daily/2025-04-25-count-of-interesting-subarrays.js @@ -0,0 +1,49 @@ +// Count of Interesting Subarrays +// Created on 2025-04-25 + +/** + * @param {number[]} nums + * @param {number} modulo + * @param {number} k + * @return {number} + */ +function solution(nums, modulo, k) { + //My soulution + // let count = 0; + // for (let i = 0; i < nums.length; i++) { + // let cnt = 0; + // for (let j = i; j < nums.length; j++) { + // if (nums[j] % modulo === k) { + // cnt++; + // } + // if (cnt % modulo === k) { + // count++; + // } + // } + // } + // return count; + + // Optimized solution + let count = 0; + let prefix = 0; + const map = new Map(); + map.set(0, 1); + + for (const num of nums) { + if (num % modulo === k) { + prefix += 1; + } + const remainder = prefix % modulo; + const want = (remainder - k + modulo) % modulo; + + if (map.has(want)) { + count += map.get(want); + } + + map.set(remainder, (map.get(remainder) || 0) + 1); + } + + return count; +} + +console.log(solution([3,2,4], 2, 1)) diff --git a/leetcode/daily/readme.md b/leetcode/daily/readme.md index 90f3d19..e5c1e8e 100644 --- a/leetcode/daily/readme.md +++ b/leetcode/daily/readme.md @@ -1,13 +1,26 @@ ## 🔍 Daily LeetCode Challenges -![Challenges Solved](https://img.shields.io/badge/Challenges%20Solved-1-blue) -![Last Update](https://img.shields.io/badge/Last_Update-2025--04--21-success) + +![Challenges Solved](https://img.shields.io/badge/Challenges%20Solved-2-blue) +![Last Update](https://img.shields.io/badge/Last_Update-2025--04--25-success) + 🧠 This folder contains **daily LeetCode challenges** I'm solving to improve my problem-solving and algorithmic thinking. Each day I tackle a new problem and document my approach and solution. ### Challenge List +### [Count of Interesting Subarrays](https://leetcode.com/problems/count-of-interesting-subarrays/) +- **Problem**: You are given a 0-indexed integer array `nums`, an integer `modulo`, and an integer `k`. + Your task is to find the count of subarrays that are interesting. + A subarray `nums[l..r]` is interesting if the following condition holds: + Let `cnt` be the number of indices `i` in the range `[l, r]` such that `nums[i] % modulo == k`. Then, `cnt % modulo == k`. + Return an integer denoting the count of interesting subarrays. + +- **Approach**: A brute-force solution would involve checking all possible subarrays, which leads to time limit exceeded on large inputs. To optimize, I use a prefix count of how many values satisfy num % modulo === k and store the counts modulo modulo in a HashMap. This allows to calculate in O(n) time how many previous prefix counts make the current one satisfy the condition using modular arithmetic and prefix sums. + +- **Solution File**: `daily/2025-04-25-count-of-interesting-subarrays.js` + ### [Count the Hidden Sequences](https://leetcode.com/problems/count-the-hidden-sequences/) - **Problem**: Description of the problem goes here.