# Hackerrank Java Dequeue Solution

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  integers. You need to find the maximum number of unique integers among all the possible contiguous subarrays of size .

Note: Time limit is  second for this problem.

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.

Constraints

The numbers in the array will range between .

Output Format

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

Sample Input

6 3
5 3 5 2 3 2


Sample Output

3


Explanation

In the sample testcase, there are 4 subarrays of contiguous numbers.

- Has  unique numbers.

- Has  unique numbers.

- Has  unique numbers.

- Has  unique numbers.

In these subarrays, there are  unique numbers, respectively. The maximum amount of unique numbers among all possible contiguous subarrays is .

### Solution in java8

Approach 1.

import java.util.*;

public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
HashSet<Integer> set = new HashSet<>();

int n = in.nextInt();
int m = in.nextInt();
int max = Integer.MIN_VALUE;

for (int i = 0; i < n; i++) {
int input = in.nextInt();

if (deque.size() == m) {
if (set.size() > max) max = set.size();
int first = deque.remove();
if (!deque.contains(first)) set.remove(first);
}
}

System.out.println(max);
}
}

Approach 2.

    import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
Set<Integer> set = new HashSet<>();
int n = in.nextInt();
int m = in.nextInt();
int max = Integer.MIN_VALUE;

for (int i = 0; i < n; i++) {
int num = in.nextInt();

if (deque.size() == m) {
if (set.size() > max)
max = set.size();

int first = deque.remove();
if (!deque.contains(first))
set.remove(first);
}

}

System.out.println(max);

}

}
{"ft_lang_label":"__label__en","ft_lang_prob":0.5894017,"math_prob":0.97685015,"size":2561,"snap":"2023-40-2023-50","text_gpt3_token_len":596,"char_repetition_ratio":0.11967149,"word_repetition_ratio":0.37157106,"special_character_ratio":0.2709879,"punctuation_ratio":0.2019802,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9967274,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-30T20:05:08Z\",\"WARC-Record-ID\":\"<urn:uuid:29d5b30e-24c2-4814-a611-9304dd5be854>\",\"Content-Length\":\"44994\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d7ad1dfd-491a-4df9-8dab-9735743caf03>\",\"WARC-Concurrent-To\":\"<urn:uuid:4bebc6f7-ebc1-42de-be92-dc3bb612e3be>\",\"WARC-IP-Address\":\"104.21.39.184\",\"WARC-Target-URI\":\"https://www.thepoorcoder.com/hackerrank-java-dequeue-solution/\",\"WARC-Payload-Digest\":\"sha1:POXHNPPXFAOEQMIKJ7ITLTEP42ONNO4C\",\"WARC-Block-Digest\":\"sha1:X2TMG5SELPS5QQKBQXKUW3GIMYACGDHT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510707.90_warc_CC-MAIN-20230930181852-20230930211852-00582.warc.gz\"}"}