Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions leetcode/daily/2025-04-25-count-of-interesting-subarrays.js
Original file line number Diff line number Diff line change
@@ -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))
17 changes: 15 additions & 2 deletions leetcode/daily/readme.md
Original file line number Diff line number Diff line change
@@ -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
<!-- NEW_CHALLENGES_HERE -->
### [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.
Expand Down