#K79577. Dynamic Array Operations
Dynamic Array Operations
Dynamic Array Operations
This problem requires you to implement a dynamic array that supports several operations. You are to design a data structure for a dynamic array of integers that supports the following operations efficiently:
- append x: Append an integer x to the end of the array.
- remove i: Remove the integer at index i (0-indexed).
- get i: Get and output the integer at index i.
- sum: Compute and output the sum of all integers in the array.
Initially, the array is empty. The operations will be provided sequentially. For every get
or sum
operation, output the result on a separate line.
Example:
Input: 7 append 1 append 2 append 3 get 1 remove 1 get 1 sum</p>Output: 2 3 4
inputFormat
The first line contains an integer T representing the number of operations. The following T lines each contain an operation in one of the following formats:
append x
— wherex
is an integer to append.remove i
— wherei
is a 0-indexed position to remove.get i
— wherei
is the index whose value is to be printed.sum
— which requires printing the sum of all integers in the array.
outputFormat
For every get
and sum
operation in the input, print the corresponding result on a new line.
7
append 1
append 2
append 3
get 1
remove 1
get 1
sum
2
3
4
</p>