Input Format

The first line of input contains two integers  and : representing the total number of integers and the size of the subarray, respectively. The next line contains  space separated integers.


The numbers in the array will range between .

Output Format

Print the maximum number of unique integers among all possible contiguous subarrays of size .

Hackerrank java dequeue solution

Java Dequeue problem solution | HackerRank

import java.util.*;
public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(;
		Deque<Integer> deque = new ArrayDeque<Integer>();
		HashMap<Integer, Integer> hashmap = new HashMap<Integer, Integer>();
		int n = in.nextInt();
		int m = in.nextInt();
		int ans = 0, distinct = 0;
		for (int i = 0; i < n; i++) {
			int num = in.nextInt();
			if (hashmap.get(num) == null) hashmap.put(num,0);
			hashmap.put(num, hashmap.get(num)+1);
			if (hashmap.get(num)==1) distinct++;
			if (deque.size() == m+1){
				int ele = deque.removeFirst();
				hashmap.put(ele, hashmap.get(ele)-1);
				if (hashmap.get(ele) == 0) distinct--;
			if (deque.size() == m){
				if (distinct > ans) ans = distinct;