Declaring the length of the array after taking the input

Revision en1, by dancingwind, 2023-01-11 07:50:23

I am a newbie who has been trying to improve at competitive programming and algorithmic thinking. Solving some problems and looking at several people's codes, I noticed a weird contrast between my habit and that of others.

In quite a bit of problems, we usually need to take an array (or whatever other data structure) of elements, and most of the time the number of elements is given first. Actually, what I am used to doing is ~~~~~ int n; cin>>n; int a[n]; ~~~~~

On the other hand, I saw some people doing

int n;
int a[some_limit];

where some_limit is often the limit of n in all test cases as given in the problem description.

I wonder what kind of differences lie between those two approaches. Can they be interchangeably used and have almost the same time and space complexity? Are they equivalent as long as we ignore the difference between the time and space complexities?

Tags array limit, input limit, input reading, arrays

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English dancingwind 2023-01-11 09:35:21 2 Tiny change: 'doing is\n~~~~~\ni' -> 'doing is\n\n~~~~~\ni'
en1 English dancingwind 2023-01-11 07:50:23 978 Initial revision (published)