-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctSubarrays.java
More file actions
34 lines (29 loc) · 1.07 KB
/
DistinctSubarrays.java
File metadata and controls
34 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.*;
import java.util.HashSet;
import java.util.StringTokenizer;
public class DistinctSubarrays {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
long count = 0;
int l = 0;
HashSet<Integer> set = new HashSet<>();
for (int right = 0; right < n; right++) {
while (set.contains(arr[right])) {
set.remove(arr[l]);
l++;
}
set.add(arr[right]);
count += (long) (right - l + 1);
}
pw.println(count);
pw.flush();
}
}