-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17_4.java
39 lines (28 loc) · 854 Bytes
/
day17_4.java
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
//Given an array of integers and a number K. Find the count of distinct elements in every window of size K in the array.
class Solution
{
ArrayList<Integer> countDistinct(int A[], int n, int k)
{
// code here
ArrayList<Integer> ans = new ArrayList<>();
HashMap<Integer,Integer> map = new HashMap<>();
int i=1;
int c = k;
for(int j=0;j<c;j++){
map.put(A[j],map.getOrDefault(A[j],0)+1);
}
ans.add(map.size());
while(i>=1 && i<=n-k)
{
map.put(A[i-1],map.get(A[i-1])-1);
if(map.get(A[i-1])==0){
map.remove(A[i-1]);
}
map.put(A[c],map.getOrDefault(A[c],0)+1);
c++;
ans.add(map.size());
i++;
}
return ans;
}
}