Had an interesting question today that I am still not sure how to approach:
You are given an array of positive/negative numbers that are not necessarily distinct. We have to return the number of contiguous subarrays that contain at least one of each number (i.e. only duplicates).
Examples:
[0, 0, 0] => 3: first two 0s, second and third 0, and entire array
[1, 2, 3] => 0
[1, 2, 3, 3, 3, 2, 4, 1] => 4: [3, 3] (first), [3, 3] (second), [3, 3, 3], [2, 3, 3, 3, 2]
[1, 2, 1, 2] => 1: only the entire array works herearr.length <= 10^3Tried to approach this with sliding window, but struggled with coming up with non-brute force solution.