Size and capacity
# Topic
Dynamic array
# Problem
Size and capacity
Assume that for a dynamic array of size 22 and capacity 44 we perform the following sequence of operations:
add 7
add 1
add 3
insert 4 3
add 6
add 7
add 8
1
2
3
4
5
6
7
2
3
4
5
6
7
Here "add x" means "add an element x to the end of the array" and "insert x i" means "add an element x at the specified index i".
Once all operations are performed, what will be the difference between the capacity of the new array (new_capacity) and its size (new_size)? Print this number in the field below.
Assume that the scaling factor for the array is 22. Also note that every time you increase the array's capacity, it becomes twice as big as the capacity of the array at the previous step, rather than the initial array's capacity.
Enter a number:
7
Correct.
# Hint & Explain
// The task only confuses, but does not help !
add(7); // size == 3; capacity == 4 add(1); // size == 4; capacity == 4 add(3); // size == 5; capacity == 8 insert (3, 4); // size == 6; capacity == 8 !!! Insert !! not set. The array is being shifted add(6); // size == 7; capacity == 8 add(7); // size == 8; capacity == 8 add(8); // size == 9; capacity == 16 // size == 9; capacity == 16 // 16 - 9 = ?
编辑 (opens new window)
上次更新: 2022/09/25, 10:41:23