-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathEquivalent Sub-Arrays.java
More file actions
44 lines (39 loc) · 1 KB
/
Equivalent Sub-Arrays.java
File metadata and controls
44 lines (39 loc) · 1 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
class Solution
{
static int countDistinctSubarray(int arr[], int n)
{
Set<Integer> set = new HashSet<>();
HashMap<Integer,Integer> mp = new HashMap<>();
for(int i=0;i<n;i++)
set.add(arr[i]);
int dn = set.size();
set.clear();
if(dn == n) return 1;
for(int i=0;i<dn;i++)
mp.put(arr[i],mp.getOrDefault(arr[i],0)+1);
int l=0, r=dn-1, count = 0;
while(l <= (n-dn))
{
if(mp.size() == dn)
{
count += n-r;
mp.put(arr[l],mp.get(arr[l])-1);
if(mp.get(arr[l]) == 0)
{
mp.remove(arr[l]);
}
l++;
}
else if(r < n-1)
{
r++;
mp.put(arr[r],mp.getOrDefault(arr[r],0)+1);
}
else
{
break;
}
}
return count;
}
}