GATE Computer ScienceVery hardGATE CS

A dynamic array starts with capacity 1. Whenever an append finds it full, a new array of double the capacity is allocated and all current elements are copied. After 1000 appends, how many element copies have occurred in total because of resizing?

  1. A. 1023
  2. B. 1000
  3. C. 2047
  4. D. 1536

Correct answer: A. 1023

Resizing occurs when appending elements 2, 3, 5, 9, ..., 513, copying 1, 2, 4, ..., 512 elements. The total is 2^10 − 1 = 1023, which is why the amortised cost per append is O(1).

More Programming and Data Structures (GATE) questions

Exams that ask this topic