-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaDeque.java
More file actions
73 lines (55 loc) · 1.95 KB
/
JavaDeque.java
File metadata and controls
73 lines (55 loc) · 1.95 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Java Deque
In computer science, a double-ended queue (dequeue, often abbreviated to deque, pronounced deck) is an
abstract data type that generalizes a queue, for which elements can be added to or removed from either the
front (head) or back (tail).
Deque interfaces can be implemented using various types of collections such as LinkedList or ArrayDeque
classes. For example, deque can be declared as:
Deque deque = new LinkedList<>();
or
Deque deque = new ArrayDeque<>();
You can find more details about Deque here.
In this problem, you are given N integers. You need to find the maximum number of unique integers among
all the possible contiguous subarrays of size M.
Note: Time limit is 3 second for this problem.
Input Format
The first line of input contains two integers N and M: representing the total number of integers and
the size of the subarray, respectively. The next line contains N space separated integers.
Constraints
1≤N≤100000
1≤M≤100000
M≤N
The numbers in the array will range between [0,10000000].
Output Format
Print the maximum number of unique integers among all possible contiguous subarrays of size M separated by a space.
Sample Input
6 3
5 3 5 2 3 2
Sample Output
3
*/
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque deque = new ArrayDeque<Integer>();
int n = in.nextInt();
int m = in.nextInt();
int maxUnique = 0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
if(i == 0){
deque.add(num);
maxUnique++;
}else{
if(deque.size() == m){
deque.removeFirst();
}
if(!deque.contains(num) && maxUnique<m){
maxUnique++;
}
deque.addLast(num);
}
}
System.out.println(""+maxUnique);
}
}