-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSlidingWindowTechnique.java
More file actions
52 lines (48 loc) · 1.22 KB
/
Copy pathSlidingWindowTechnique.java
File metadata and controls
52 lines (48 loc) · 1.22 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
49
50
51
52
import java.util.Scanner;
class SlidingWindowTechnique{
static int Nmax = 100000;
static int[] fr = new int[Nmax];
static int slidingWindow(int[] a, int n, int k){
int ans = 0, cnt = 0, Right;
for(Right=1 ; Right<=n; Right++){
if(++fr[a[Right]] == 1)
cnt++;
if(cnt >k)
break;
}
if(cnt<=k)
return n;
/// 1 2 3 4 5 , for left=1 and k=2, right=3, maxR[1] = right -1 =2
if(--fr[a[Right--]] == 0)
cnt--;
for(int Left=2 ; Left <=n; Left++){
if(--fr[a[Left - 1]] == 0)
cnt--;
while(Right < n){
if(++fr[a[++Right]] == 1)
cnt++;
if(cnt > k)
break;
}
if(cnt <= k)
return Math.max(ans , n-Left+1);
if(--fr[a[Right--]] == 0)
cnt--;
ans = Math.max(ans, Right-Left+1);
}
return ans;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] a = new int[Nmax];
int n, k;
System.out.println("Enter the number of elements : ");
n = sc.nextInt();
System.out.println("Enter the number of distinct elements :");
k = sc.nextInt();
System.out.println("Enter the array elements :");
for(int i=1 ; i<=n; i++)
a[i] = sc.nextInt();
System.out.println(slidingWindow(a , n ,k));
}
}