Skip to content
Image
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Java Dequeue problem solution

Image YASH PAL, 31 July 202416 January 2026

HackerRank Java Dequeue problem 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 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.

HackerRank Java Dequeue problem solution

HackerRank Java Dequeue problem solution.

import java.util.*;

public class test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        final Deque<Integer> deque = new ArrayDeque<Integer>();
        final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        final int n = in.nextInt();
        final int m = in.nextInt();

        int res = 0;
        for (int i = 0; i < n; i++) {
            final int num = in.nextInt();
            deque.addLast(num);
            if (map.containsKey(num)) {
                map.put(num, map.get(num).intValue() + 1);
            } else {
                map.put(num, 1);
            }

            if (deque.size() == m + 1) {
                final int key = deque.removeFirst();
                final int v = map.get(key);
                if (v == 1) {
                    map.remove(key);
                } else {
                    map.put(key, v - 1);
                }
            }

            final int cnt = map.size();
            if (cnt > res) { res = cnt; }
        }
        System.out.println(res);
    }
}

Second solution

    import java.util.*;
    public class test {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Deque<Integer> deque = new ArrayDeque<Integer>();
            HashMap<Integer, Integer> freqs = new HashMap<Integer, Integer>();
            int n = in.nextInt();
            int m = in.nextInt();
            int ans = 0, countDistinct = 0;
            
            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                deque.addLast(num);
                if (freqs.get(num) == null) freqs.put(num,0);
                
                freqs.put(num,freqs.get(num)+1);
                if (freqs.get(num)==1) countDistinct++;
                
                if (deque.size()==m+1){
                    int rem = deque.removeFirst();
                    freqs.put(rem,freqs.get(rem)-1);
                    if (freqs.get(rem) == 0) countDistinct--;
                }
                if (deque.size()==m){
                    if (countDistinct > ans) ans = countDistinct;
                }
               
            }
            System.out.println(ans);
        }
    }

A solution in java8 programming.

    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();
            
            deque.add(input);
            set.add(input);
            
            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);
    }
}

coding problems solutions Hackerrank Problems Solutions Java solutions HackerRankjava

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank Java Solutions
Welcome to Java! problem solution
Java Stdin and Stdout I problem solution
Java If-Else problem solution
Java Stdin and Stdout II problem solution
Java Output Formatting problem solution
Java Loops I problem solution
Java Loops II problem solution
Java Datatypes problem solution
Java End-of-file problem solution
Java Static Initializer Block solution
Java Int to String problem solution
Java Date and Time problem solution
Java Currency Formatter problem solution
Java Strings Introduction problem solution
Java Substring problem solution
Java Substring Comparisons problem solution
Java String Reverse problem solution
Java Anagrams problem solution
Java String Tokens problem solution
Pattern Syntax Checker solution
Java Regex problem solution
Java Regex 2 — Duplicate Words solution
Valid Username Regular Expression solution
Java ArrayList problem solution
Java BigDecimal problem solution
Java Primality Test problem solution
Java BigInteger problem solution
Java 1D Array problem solution
Java 2D Array problem solution
java Subarray problem solution
Java Arraylist problem solution
Java 1D Array (Part 2) problem solution
Java List problem solution
Java Map problem solution
Java Stack problem solution
Java Hashset problem solution
Java Generics problem solution
Java Comparator problem solution
Java Sort problem solution
Java Dequeue problem solution
Java BitSet problem solution
Java Priority Queue problem solution
Java Inheritance I problem solution
Java Inheritance II problem solution
Java Abstract Class problem solution
Java Interface problem solution
Java Method Overriding problem solution
Java Method Overriding 2 (Super Keyword) solution
Java Instanceof keyword problem solution
Java Iterator problem solution
Java Exception Handling (Try-catch) solution
Java Exception Handling problem solution
Java Varargs — Simple Addition solution
Java Reflection — Attributes problem solution
Can You Access? problem solution
Prime Checker problem solution
Java Factory Pattern problem solution
Java Singleton Pattern problem solution
Java Visitor Pattern problem solution
Java Annotations problem solution
Covariant Return Types solution
Java Lambda Expressions problem solution
Java MD5 problem solution
Java SHA-256 problem solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes
Advertisement