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.
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.