When items are removed from the middle of a sequence, the positions of all
the items further down the sequence change. We are given a sequence and a
list of removals that will occur. We want to be able to determine
which item ends up in a specified position.
We refer to the positions in our sequence starting with 1. Let the items in our sequence
initially be 1, 2, ..., n. Let k be the specified final position, and
let remove be a list of Strings that gives the
removals in order. Each removal is in the form "lo-hi" where lo and hi
are positive integers (with no leading zeros) giving the range of positions
whose items are to be removed. Each
removal refers to the items by current position (not original position) in the
sequence and includes both lo and hi. So if n is 8 and removal is {"3-4","4-5"} the sequence after the
first removal is 1, 2, 5, 6, 7, 8 and the final sequence is 1, 2, 5, 8.
Create a class Removal that contains a method finalPos that takes as input n,
the original sequence length, k the final position of interest,
and remove, a String[] of removals formatted as described above.
The method returns the item that ends up in position k, or returns -1 if no item
ends up in position k (i.e. if there are fewer than k items left after all the
removals).
|