I'm Telecommunication student so I don't have Computer Science background, just in case if my question looks stupid.
Here is my scenario:
Consider an empty array named
ARRwith sizeN, we also have an array namedMAPwith sizeNthat stores the state ofARRcells,ONfor filled,OFFfor empty, initiallyARRis empty so every corresponding cell inMAPis set toOFF.we keep the next empty cell index in a variable named
NEXT, initially sinceARRis empty,NEXTis set to 0.NEXTalways point to the first indexiin the arrayMAPsuch thatMAP[i]isOFF, if any exists.a function named
add(element)always adds element intoARRat indexNEXT, sets corresponding cell inMAPtoON.a function named
insert(element, index)adds element intoARRat index specified by user, sets corresponding cell inMAPtoON.a function named
delete(index)removes element fromARRat index specified by user, sets corresponding cell inMAPtoOFF.
My question:
How to update NEXT variable so it points to next empty cell index in ARR.
I know I can run a search on MAP from 0 to N and stop at the first occurrence of OFF and store its index as NEXT, but from what I read from online sources it has O(N) performance which is not great, I want to know if there are any better algorithms for this job or not, if not, can I design one myself? which books/online doc should I look into?
Thanks.
NEXTalways point to the first index $i$ in the arrayMAPsuch thatMAP[i]isOFF, if any? $\endgroup$