Add function to count distinct elements in every window of size k #87
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.