countDistinctSlices.md
July 4, 2025 ยท View on GitHub
Description: Given an array of integers and an integer M, count the number of distinct slices (contiguous subarrays with all unique elements) in the array. If the result exceeds 1,000,000,000, return 1,000,000,000.
Examples
Example 1:
Input: m = 6, numbers = [3, 4, 5, 5, 2] Output: 9
Example 2:
Input: m = 5, numbers = [1, 2, 3, 4] Output: 10
Example 3:
Input: m = 2, numbers = [1, 1, 1, 1] Output: 4
Example 4:
Input: m = 3, numbers = [0, 1, 0, 2, 0] Output: 10
Algorithmic Steps This problem is solved using a sliding window and a boolean array to track seen elements:
- Initialize a boolean array of size M+1 to track seen elements.
- Use two pointers, left and right, to define the window.
- Expand the window by moving right, marking elements as seen.
- If a duplicate is found, move left to shrink the window until the duplicate is removed.
- For each step, add the window size to the total count.
- If the count exceeds 1,000,000,000, return 1,000,000,000.
- Return the total count.
Time and Space complexity:
- Time complexity:
O(n), wherenis the number of elements in the array. - Space complexity:
O(M)for the boolean array.