Input: insert[] = [ 1, 2, 3, 4, 5, 2, 3, 1 ] , findFreq[] = [ 1, 3, 2, 9, 10 ]
Output:
2
2
2
-1
-1
Explanation: After inserting 1, 2, 3, 4, 5, 2, 3, 1 into the queue, frequency of 1 is 2, 3 is 2, 2 is 2, 9 is -1 and 10 is -1 (since, 9 and 10 is not there in the queue).
Input: insert[] = [ 1, 2, 1, 1, 1, 4 ] , findFreq[] = [ 1, 5, 4, 3 ]
Output:
4
-1
1
-1
Explanation: After inserting 1, 2, 1, 1, 1 and 4 into the queue, frequency of 1 is 4 and that of 4 is 1. Since 5 and 3 are not there in the queue we output -1 for them
Illustration of findFrequency(k) :
k = 10
q = [10, 20, 30, 10]
Initialize : Frequency of k, res = 0
After pop() and push()
q = [20, 30, 10, 10], res = 1
After pop() and push()
q = [30, 10, 10, 20], res = 1
After pop() and push()
q = [10, 10, 20, 30], res = 1
After pop() and push()
q = [10, 20, 30, 10], res = 2