Java Dequeue Hackerrank Solution

 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);
        }
    }



Comments

Popular posts from this blog

Print Prime Numbers Hackerrank Solution - PL/SQL

how to store html form data in mysql database using hibernate