We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My code is having only one for loop, still I'm getting time out for test cases:8,9,10. Can anyone help me out?
Scanner in = new Scanner(System.in);
Deque deque = new ArrayDeque<>();
int n = in.nextInt();
int m = in.nextInt();
int count=0;
int ans=0;
for (int i =0; i < n; i++) {
int num=in.nextInt();
if(deque.contains(num))
count=deque.size();
else{
count=deque.size()+1;
deque.addLast(num);
if(i>=m-1)
deque.pollFirst();
}
if(count>ans)
ans=count;
}
System.out.println(ans);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Dequeue
You are viewing a single comment's thread. Return to all comments →
My code is having only one for loop, still I'm getting time out for test cases:8,9,10. Can anyone help me out?