Skip to main content

Java Dequeue

Problem
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<>();

In this problem, you are given NN integers. You need to find the maximum number of unique integers among all the possible contiguous subarrays of size MM.

Note: Time limit is 33 second for this problem.

Input Format

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

Constraints

1≤N≤1000001≤N≤100000
1≤M≤1000001≤M≤100000
M≤NM≤N
The numbers in the array will range between [0,10000000][0,10000000].

Output Format

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

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.

s1=⟨5,3,5⟩s1=⟨5,3,5⟩ - Has 22 unique numbers.

s2=⟨3,5,2⟩s2=⟨3,5,2⟩ - Has 33 unique numbers.

s3=⟨5,2,3⟩s3=⟨5,2,3⟩ - Has 33 unique numbers.

s4=⟨2,3,2⟩s4=⟨2,3,2⟩ - Has 22 unique numbers.

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

Solution
    import java.util.*;
    public class test {
       public static void main(String[] args){
           
    Scanner in = new Scanner(System.in);
    Deque q    = new ArrayDeque<Integer>();
    int []a = new int[10000001];
    int i = 0, n = in.nextInt(), m = in.nextInt();
    int r = 0, c = 0;
           
    for(;i<m;i++){
    int x = in.nextInt();
    if(a[x]==0){c++;if(r<c)r=c;}
    a[x]++;
    q.addLast(x);
    }
           
    for(;i<n;i++){
    if(a[(int)q.getFirst()]==1)c--;
    a[(int)q.getFirst()]--;
    q.removeFirst();
    int x=in.nextInt();
    if(a[x]==0){c++;if(r<c)r=c;}
    a[x]++;
    q.addLast(x);
    }
           
    System.out.println(r);
    }
    }

Comments

Popular posts from this blog

ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ ഭയങ്കര പവർഫുൾ ആണ്.

ജാവ യെ  കുറിച്ച്  മലയാളത്തിൽ  ഡോകുമെന്റുകളോ  ബ്ലോഗ്‌കളോ  കൂടുതൽ ഇല്ലാത്തതുകൊണ്ട് എനികറിയവുന്നത് ഷെയർ ചെയ്യാം എന്ന് വിചാരിച്ചു .  ജവയെ കുറിച്ച് പറയുകയാണെങ്കിൽ  ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ  ഭയങ്കര പവർഫുൾ ആണ്. :) 1. നമ്മടെ Sun MicroSystems 1995 ൽ തുടങ്ങി വച്ചാ സംഭവമാണിത് . 2. ഇപ്പൊ ജാവയുടെ എട്ടാമത്തെ വെർഷൻ  വരെ ഇറങ്ങികഴിഞ്ഞു . ജാവയുടെ തിയറിയെ കുറിച്ച് എനിക്ക് പറയാൻ താല്പര്യം  ഇല്ലാ, അത് അറിവിള്ളവർ നല്ലപോലെ പറഞ്ഞിടുണ്ട്. നമുക്ക് ഡയറക്റ്റ്  ആയി കോഡിംഗ് പാർട്ടിലേക്ക്  പോകാം. എങ്ങനെയാ ജാവ ഇൻസ്റ്റോൾ ചെയണ്ടേ എന്നൊക്കെ ഒന്ന് ഗൂഗിൾനോട് ചോദിച്ചാ മതി പുള്ളി പറഞ്ഞുതരും. പിന്നെ എല്ലാവരും പറയുന്നു ജാവാ Object  Oriented ആണെന്ന് . അതെ ജാവാ Object Oriented ആണ് . എന്താണ് Object ?, Object  നു വച്ചാ ഒരു സാധനം അത്രതന്നെ. ഇപ്പൊ example പറയുകയാണെങ്കിൽ Book ഒരു സാധനമാണ് . Book റ്റെ പ്രതേകത എന്തൊക്കെ അന്നെന്നുവച്ചാ ആതിനൊരു Auther ഉണ്ടാകും, അതൊരു Category ൽ പെട്ട book ആയിരിക്കും (Novel / Academic ) പിന്നെ ഓരോ bookനും അ...

Transform Ubuntu into Xubuntu

Installation 1. Open the terminal by Ctrl+Alt+T     and type    sudo apt-get install xubuntu-desktop gksu leafpad synaptic         2. Type your password & Press Enter. Now an intensive operation is being launched. Simply wait to complete the whole process. Login To Xubuntu 1. After completing the installation logout ubuntu. Note: logout not restart or shutdown. 2. In the login window click on the ubuntu logo, next to your userName & select Xubuntu Sesion 3. Enter your password and Now the Xubuntu desktop appears. :) The next thing is to clean up. Clean Up 1. Now its time to clean up, inorder to prevent system pollution problems. Note: The clean up will remove as much as possible ubuntu's desktop environment Unity. So after that you can't use Unity. 2. Open terminal by Ctrl+Alt+T and type the following  sudo apt-get remove nautilus gnome-power-manager gnome-screensaver gnome-termina*...

Object serialization in java

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object. When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Code for serialization of a java class : Data.java package com . codetalk . serialization ; import java.io.Serializable ; public class Data implements Serializable { private static final long serialVersionUID = 1L; private String firstName ; private String lastName ; /** * @return the firstName */ public String getFirstName () { return firstName ; } /** * @param firstNam...