-
Notifications
You must be signed in to change notification settings - Fork 33
[NEW] Add solution for Distinct Values Subarrays II #142
Copy link
Copy link
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
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
🎯 Problem Information
📋 Problem Description
You are given an array of
nintegers and a numberk.Your task is to count the number of subarrays that contain at most
kdistinct 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:
🏷️ Labels
Please add appropriate labels:
hacktoberfestgood first issuehelp wanted👤 Assignee
📎 Additional Context