We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import java.util.*;
import java.util.stream.Collectors;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
Map<Integer,Integer> cnt = new HashMap<>();
int n = in.nextInt();
int m = in.nextInt();
int mx = 0;
in.nextLine();
List<Integer> A = Arrays.stream(in.nextLine().trim().split("\\s+"))
.map(Integer::parseInt).collect(Collectors.toList());
for (int i = 0; i < n; i++) {
deque.addLast(A.get(i));
cnt.put(A.get(i),cnt.getOrDefault(A.get(i), 0)+1);
if (deque.size()>m) {
int rm = deque.removeFirst();
cnt.put(rm,cnt.get(rm)-1);
if (cnt.get(rm) == 0) cnt.remove(rm);
}
if (deque.size()== m) mx = Math.max(mx,cnt.size());
}
System.out.println(mx);
}
}
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<Integer>();
int n = in.nextInt();
int m = in.nextInt();
int max = 0;
Set<Integer> j = new HashSet<>();
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.addLast(num);
j.add(num);
if(deque.size()==m){
int count = j.size();
if(max<count) max=count ;
int target = deque.removeFirst();
if(!deque.contains(target)) j.remove(target);
}
}
System.out.println(max);
}
}
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
int n = in.nextInt();
int m = in.nextInt();
ArrayList<Integer> sizeArr = new ArrayList<>();
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.add(num);
}
//change to list so that it can be easily sublisted
List<Integer> list = new ArrayList<Integer>(deque);
//sub-queue loop
for (int i = 0; i <= n-m; i++) { //farthest index to start subqueue will be "n-m" (to n)
HashSet<Integer> hs = new HashSet<>(list.subList(i, i+m));
sizeArr.add(hs.size());
}
System.out.println(Collections.max(sizeArr));
}
}
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
Map<Integer, Integer> map = new HashMap<>();
int n = in.nextInt();
int m = in.nextInt();
int res = 0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.add(num);
map.put(num, map.getOrDefault(num, 0)+1);
if(deque.size()==m){
res = Math.max(res, map.size());
int frontElement = deque.poll();
map.put(frontElement, map.get(frontElement)-1);
if(map.get(frontElement)==0){
map.remove(frontElement);
}
}
}
System.out.println(res);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Please Login in order to post a comment