Array Declaration and Initialization in DSA C - Time & Space Complexity
When we create and fill an array, we want to know how long it takes as the array gets bigger.
We ask: How does the time to set up the array grow when the number of elements grows?
Analyze the time complexity of the following code snippet.
int n = 5;
int arr[5];
for (int i = 0; i < n; i++) {
arr[i] = i * 2;
}
This code creates an array of size 5 and fills it with values by multiplying the index by 2.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that assigns values to each array element.
- How many times: It runs once for each element, so n times.
As the array size grows, the number of assignments grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The time grows directly with the number of elements; doubling n doubles the work.
Time Complexity: O(n)
This means the time to fill the array grows in a straight line with the number of elements.
[X] Wrong: "Declaring an array takes no time or is instant regardless of size."
[OK] Correct: While declaration reserves space quickly, initializing each element takes time proportional to the array size.
Understanding how array setup time grows helps you explain efficiency clearly and shows you know how data size affects performance.
"What if we initialize the array with a fixed value instead of a calculation? How would the time complexity change?"
Practice
Solution
Step 1: Understand array declaration syntax in C
In C, to declare an array of integers with size 5, the syntax isint arr[5];.Step 2: Check each option
int arr[5]; matches the correct syntax. Options A, B, and D are incorrect because they either miss the size, use wrong keywords, or assign a value instead of declaring an array.Final Answer:
int arr[5]; -> Option DQuick Check:
Array declaration = int arr[5]; [OK]
- Forgetting to specify size inside brackets
- Using wrong keywords like 'array'
- Assigning a value instead of declaring
Solution
Step 1: Recall array initialization syntax
In C, arrays are initialized using curly braces with values inside, and size must be specified if not inferred.Step 2: Evaluate each option
int arr[3] = {1, 2, 3}; correctly declares an array of size 3 and initializes it with values 1, 2, 3. int arr = {1, 2, 3}; misses the size, C uses parentheses instead of braces, and D uses square brackets which is invalid.Final Answer:
int arr[3] = {1, 2, 3}; -> Option CQuick Check:
Array init uses braces and size = int arr[3] = {1, 2, 3}; [OK]
- Using parentheses () instead of braces {}
- Omitting array size when required
- Using square brackets [] for initialization
int arr[4] = {10, 20};
for(int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}Solution
Step 1: Understand partial initialization of arrays
When an array is partially initialized in C, the remaining elements are set to zero automatically.Step 2: Trace the loop output
arr[0] = 10, arr[1] = 20, arr[2] = 0, arr[3] = 0. The loop prints all four elements separated by spaces.Final Answer:
10 20 0 0 -> Option AQuick Check:
Partial init fills rest with 0 = 10 20 0 0 [OK]
- Assuming uninitialized elements have garbage values
- Confusing partial initialization with full initialization
- Miscounting array size in loop
int arr[3];
arr = {1, 2, 3};Solution
Step 1: Understand array assignment rules in C
In C, arrays cannot be assigned values using braces after declaration. Initialization must happen at declaration.Step 2: Check the code snippet
The code tries to assign values to arr using braces after declaration, which is invalid syntax and causes a compilation error.Final Answer:
Cannot assign values to array after declaration using braces -> Option BQuick Check:
Array assignment after declaration with {} is invalid [OK]
- Trying to assign arrays after declaration with {}
- Confusing initialization with assignment
- Using parentheses instead of braces
int arr[] = {5, 10, 15};
int *ptr = arr;
for(int i = 0; i < 3; i++) {
printf("%d ", *(ptr + i));
}Choose the correct output.
Solution
Step 1: Understand pointer and array relationship
In C, the name of the array is a pointer to its first element. Pointer arithmetic allows accessing elements by offset.Step 2: Trace the loop output
Pointer ptr points to arr[0]. *(ptr + i) accesses arr[i]. Loop prints 5, 10, 15 separated by spaces.Final Answer:
5 10 15 -> Option AQuick Check:
Pointer arithmetic accesses array elements correctly = 5 10 15 [OK]
- Confusing pointer arithmetic syntax
- Assuming pointer points to garbage
- Forgetting array size in loop
Ready to prove it?
Pass the test and this pattern moves unknown
