Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to count distinct elements in every window of size k #87

Merged
merged 1 commit into from
Oct 13, 2024
Merged

Add function to count distinct elements in every window of size k #87

merged 1 commit into from
Oct 13, 2024

Conversation

Aniketghosh2003
Copy link
Contributor

Description:
This PR introduces a function countDistinctElements that calculates the number of distinct elements in every sliding window of size k in a given array. The solution uses an unordered map to efficiently track the frequency of elements within each window, ensuring the calculation of distinct elements in optimal time.

Key Changes:
Implemented the countDistinctElements function.
The function uses an efficient sliding window technique with an unordered map to keep track of the count of distinct elements in each window.
Added test cases in the main function with an example array and window size to validate the logic.

Performance:
Time Complexity: O(n), where n is the size of the input array.
Space Complexity: O(k), where k is the size of the window, due to the unordered map storing distinct elements for each window.

Testing:
Tested with example array: {1, 2, 1, 3, 4, 2, 3} and window size k = 4.
Verified that the output is correct, returning the count of distinct elements for each sliding window.

@dubey-harshit dubey-harshit merged commit a3d1feb into dubey-harshit:main Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants