I have been solving problems in an online competitive coding forum and came across this problem : http://www.spoj.com/problems/PQUEUE/. I have implemented the same in Java and it is working fine for all the test cases I can think of. However, it is getting a NZEC when I tried to submit my code. Given below is portion of my code. The input range is well handled. So don't worry about that. Here, in
is an object of Scanner
class.
int t = in.nextInt(), i, k, count;
while (t-- > 0) {
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
ArrayList<Integer> ar = new ArrayList<Integer>();
int n = in.nextInt();
int m = in.nextInt();
for (i = 0; i < n; i++) {
k = in.nextInt();
q.offer(k);
ar.add(k);
}
Collections.sort(ar, Collections.reverseOrder());
count = 0;
int num = 0, max = ar.get(0), pos = m;
while (true) {
num = q.peek();
if (max > num) {
if (pos == 0)
pos = n - 1;
else
pos--;
q.removeFirst();
q.offer(num);
} else {
if (pos == 0)
break;
else
pos--;
count++;
q.removeFirst();
ar.remove(0);
max = ar.get(0);
}
}
System.out.println(count + 1);
Any help would be appreciated. I have been going at this code for days now, yet without any clue as to where I am getting the error. Thanks a lot in advance.