The question I'm referring to is similar to this one. The only differences are 1.) There should be a distance of atleast 'k' units from the current element to the closest smaller element that we pick. 2.) The element can be picked from either direction, towards the left or towards the right. So for example, if k=4 and there is a smaller element right next to the current one, we can't pick it as it's too close.
I tried implementing it the same way as the other solution. The change I made is, every time an element is removed from the stack, if it's actually smaller than the current element but got removed just because it's closer than k units, then I add the element back to the stack once I find the answer for the current element and then move on to the next element. This seems to work but I'm sure there is a more efficient way to solve this. Any suggestions would be very helpful.