-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountCompleteSubarraysinanArray2799.java
More file actions
48 lines (41 loc) · 1.27 KB
/
CountCompleteSubarraysinanArray2799.java
File metadata and controls
48 lines (41 loc) · 1.27 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
public class CountCompleteSubarraysinanArray2799 {
public static int countCompleteSubarrays(int[] nums) {
int totalDistinct = countDistinct(nums);
int count = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
Set<Integer> seen = new HashSet<>();
for (int j = i; j < n; j++) {
seen.add(nums[j]);
if (seen.size() == totalDistinct) {
count++;
}
}
}
return count;
}
private static int countDistinct(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
return set.size();
}
public static List<List<Integer>> findAllSubarrays(int[] arr) {
List<List<Integer>> result = new ArrayList<>();
int n = arr.length;
for (int i = 0; i < n; i++) {
List<Integer> sub = new ArrayList<>();
for (int j = i; j < n; j++) {
sub.add(arr[j]);
result.add(new ArrayList<>(sub));
}
}
return result;
}
public static void main(String[] args) {
int[] nums = { 1, 3, 1, 2, 2 };
countCompleteSubarrays(nums);
}
}