-
Notifications
You must be signed in to change notification settings - Fork 35
Closed
Labels
category: mathematicsMathematical problemsMathematical problemscategory: sorting-searchingSorting and searching problemsSorting and searching problemsenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomershacktoberfestIssues/PRs for Hacktoberfest participationIssues/PRs for Hacktoberfest participationhacktoberfest_2025Hacktoberfest 2025 specific contributionsHacktoberfest 2025 specific contributionshelp wantedExtra attention is neededExtra attention is needed
Description
🎯 Problem Information
- Problem Name: Distinct Values Subarrays II
- CSES Link: https://cses.fi/problemset/task/2428
- Category: 2_sorting_searching
- Estimated Difficulty: Medium
📋 Problem Description
You are given an array of n integers and a number k.
Your task is to count the number of subarrays that contain at most k distinct values.
💡 Proposed Approach
We can use a two-pointer (sliding window) approach with a frequency map:
-
Maintain a window
[l, r]such that it contains at mostkdistinct elements. -
Expand
rand keep track of frequencies of each element. -
When the window exceeds
kdistinct values, movelforward until it becomes valid again. -
For each
r, the number of valid subarrays ending atris(r - l + 1). -
Time Complexity: O(n)
-
Space Complexity: O(n)
📝 Implementation Notes
Any special considerations or optimizations needed:
- Requires fast I/O
- Needs modular arithmetic
- Large input constraints
- Edge cases to consider:
- All elements identical
- All elements distinct
- k = 1
- k = n
🏷️ Labels
Please add appropriate labels:
-
hacktoberfest -
good first issue -
help wanted
👤 Assignee
- I would like to work on this issue
- I'm available for guidance/review
📎 Additional Context

Metadata
Metadata
Assignees
Labels
category: mathematicsMathematical problemsMathematical problemscategory: sorting-searchingSorting and searching problemsSorting and searching problemsenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomershacktoberfestIssues/PRs for Hacktoberfest participationIssues/PRs for Hacktoberfest participationhacktoberfest_2025Hacktoberfest 2025 specific contributionsHacktoberfest 2025 specific contributionshelp wantedExtra attention is neededExtra attention is needed