Skip to content

[NEW] Add solution for Distinct Values Subarrays II #142

@Insha-7

Description

@Insha-7

🎯 Problem Information

📋 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 most k distinct elements.

  • Expand r and keep track of frequencies of each element.

  • When the window exceeds k distinct values, move l forward until it becomes valid again.

  • For each r, the number of valid subarrays ending at r is (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

Image

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions