Current implementation runs in O(n*k) time. Each element in the buffer is moved down an index one by one.
Solution
It could be possible to use a ring buffer. Instead of shifting every element down by one continuously, just change where the front and rear pointers point to. This will effectively give the illusion of shifting all the elements. This process could run in O(1) time.
Problem
Current implementation runs in O(n*k) time. Each element in the buffer is moved down an index one by one.
Solution
It could be possible to use a ring buffer. Instead of shifting every element down by one continuously, just change where the front and rear pointers point to. This will effectively give the illusion of shifting all the elements. This process could run in O(1) time.