Unique Values
InterviewTime limit1sMemory limit1024 MB
Count the contiguous subarrays of a given array in which no value appears more than once.
- Level
Medium4 of 10
- Topics
- Sliding window, Two pointers, Hash map
- Solved
- No attempts yet
Problem
Arup has to make many practice questions for his Computer Science 1 students. Many of the questions deal with arrays, so he has to generate arrays for them. He does not want to give difficult practice problems, so he always guarantees that every array given to the students has unique values. No value appears twice in any of his arrays.
Unfortunately, Arup is too lazy to generate arrays. About 20 years ago, when he started teaching Computer Science 1, he made one very long array to reuse. This long array may contain duplicate values. For each problem, he takes a contiguous subsequence of the long array as the problem's array, but the subsequence must not contain duplicates. If the long array is , a contiguous subsequence is any sequence with .
Given an array of integers, count the contiguous subsequences that do not contain any repeated values. Two subsequences with the same contents count as different if they start at different positions in the original array.
Input
The first line contains a single positive integer (), the size of the input array. The next line contains space-separated integers, the values of the array in order. Each value is an integer between and , inclusive.
Output
Print the number of contiguous subsequences that do not contain any repeated values, on a line by itself.