Java Dequeue Hackerrank Solution
For Explanation Watch Video:
Sample Input
6 3
5 3 5 2 3 2
Sample Output
3
Code:
import java.util.*; public class test { public static void main(String[] args) { Scanner in = new Scanner(System.in); Deque deque = new ArrayDeque<>(); int n = in.nextInt(); int m = in.nextInt(); int max = -1; HashSet<Integer> hs = new HashSet<>(); for (int i = 0; i < n; i++) { int input = in.nextInt();
deque.add(input); hs.add(input);
if (deque.size() == m) { if(hs.size() > max) max = hs.size();
int first = (int) deque.remove(); if (!deque.contains(first)) hs.remove(first); } } System.out.println(max); } }
if (!deque.contains(first))
ReplyDeletehs.remove(first);
what does this mean