-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistinctDifferenceArray
More file actions
31 lines (31 loc) · 1.12 KB
/
distinctDifferenceArray
File metadata and controls
31 lines (31 loc) · 1.12 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
class Solution {
public int[] distinctDifferenceArray(int[] nums) {
int n = nums.length;
int[] diff = new int[n];
for(int i=0;i<n;i++){
HashMap<Integer, Integer> count1 = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> count2 = new HashMap<Integer, Integer>();
int[] suffix = new int[n-i-1];
int[] prefix = new int[i+1];
prefix = Arrays.copyOfRange(nums, 0, i+1);
suffix = Arrays.copyOfRange(nums, i+1, n);
for (int num : prefix) {
count1.put(num, count1.getOrDefault(num, 0) + 1);
}
for (int num : suffix) {
count2.put(num, count2.getOrDefault(num, 0) + 1);
}
int difPrefix = 0, difSuffix = 0;
for(int key : count1.keySet()){
if(count1.get(key) > 0)
difPrefix++;
}
for(int key : count2.keySet()){
if(count2.get(key) > 0)
difSuffix++;
}
diff[i] = difPrefix - difSuffix;
}
return diff;
}
}