EPI Arrays - Problem 5.2 Increment an arbitrary precision integer

#1

I recently purchased the C++ version of the book. I see that in every code for arrays, a vector is taken as an input instead of arrays. Why is it so?

Also, in problem 5.2, resizing is performed for cases where the number of digits increases. But the main difference between a vector and array is that arrays are of fixed length. Doing the same taking array as an input would increase the space complexity to O(n).

I have seen similar problems for vectors or strings, like adding two numbers. Maybe this is mis-categorized into arrays, or am I missing something here?

0 Likes

#2

Hi @yo23,

Thanks for your question, and the reason we use vector as input is because that is what C++ “array” shall be. The other thing we can use is std::array, which is a fixed-size one which is not flexible to show the algorithm.

I think for Problem 5.2, the space complexity is always O(n) if you consider the output as part of space no matter you use array or vector.

Conceptually, I would say array and strings are very similar as string as just array of characters. Feel free to let me know what problems you think we are mis-categorized, and we are open to discuss here.

0 Likes