Why is there a TLE in some cases?

What is the mistake in my code? why is it giving a TLE in some cases

@anushreemahur5 don’t pop back from your queue when you get equal value else when there are multiple equal value you while loop will run longer and wil give you TLE
i have updated it in your code and commented that part loo here https://ide.codingblocks.com/s/243881

Doing int a[n] will give segmentation fault , so do i dynamically allocate the memory to array?

It doesnt give a segmentation fault .

Still for one testcase it is TLE, why so ?

@anushreemahur5,
Declare the array a globally, rather than dynamically, also no need of queue q(k), queue q; is enough.