ProgrammingVery hard

Growing a dynamic array by a fixed 1 element every time it fills (capacity + 1) costs what in the worst case for N pushes?

  1. A. About N copies in total, so pushes are O(1) each
  2. B. About N log N copies, like merge sort
  3. C. About N^2 / 2 element copies in total, so pushes are O(N) each on average
  4. D. About N copies only when the array is shrunk

Correct answer: C. About N^2 / 2 element copies in total, so pushes are O(N) each on average

Each growth copies the whole array, so the copies sum to 1 + 2 + ... + N, which is about N^2 / 2. Doubling avoids this.

More C Programming questions

Exams that ask this topic