#C8264. Circular Buffer Commands
Circular Buffer Commands
Circular Buffer Commands
You are required to implement a circular buffer that supports three types of commands: append, get, and remove. The circular buffer has a fixed capacity. When the buffer is full and a new element is appended, the oldest value in the buffer is overwritten.
The commands are defined as follows:
- append X: Append the integer X to the buffer. If the buffer is already full, the oldest value will be replaced.
- get I: Retrieve the value at index I (0-indexed). If the index is invalid (i.e. I is less than 0 or greater than or equal to the current size of the buffer), output
Invalid index
. - remove I: Remove the value at index I from the buffer. If the index is invalid, output
Invalid index
as a result.
The implementation should follow the concept of a circular buffer. In particular, if we denote the fixed capacity as \(C\) and the number of elements currently in the buffer as \(N\), then a valid index must satisfy \(0 \le I < N\).
inputFormat
The input is given via stdin and is formatted as follows:
- The first line contains two space-separated integers: Q (the number of commands) and capacity (the fixed size of the circular buffer).
- The next Q lines each contain a command. A command is either:
append X
,get I
, orremove I
where X and I are integers.
Note: If the capacity is 0, all get
and remove
commands should output Invalid index
.
outputFormat
Print the results to stdout on a single line, with each result separated by a space. Only the results of a get
command or a remove
command that returns an error (Invalid index
) should be output.
7 3
append 1
append 2
append 3
append 4
get 0
get 2
remove 1
2 4