Array initialization in C++ - Time & Space Complexity
When we create and fill an array, the time it takes depends on how many items we add.
We want to know how the work grows as the array size grows.
Analyze the time complexity of the following code snippet.
int arr[1000];
for (int i = 0; i < 1000; i++) {
arr[i] = 0;
}
This code creates an array of 1000 integers and sets each element to zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Setting each array element to zero.
- How many times: Exactly 1000 times, once for each element.
As the array size grows, the number of times we set elements grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly in proportion to the number of elements.
Time Complexity: O(n)
This means the time to initialize the array grows linearly with the number of elements.
[X] Wrong: "Initializing an array is instant and does not depend on size."
[OK] Correct: Each element must be set, so the time grows with the number of elements.
Understanding how array initialization scales helps you reason about performance in many programs.
"What if we initialize the array with a fixed value using a built-in function? How would the time complexity change?"
Practice
Solution
Step 1: Understand array initialization with empty braces
Using empty braces {} initializes all elements to zero by default in C++.Step 2: Compare options
int arr[5] = {}; uses {} which sets all elements to zero. int arr[5] = 0; is invalid syntax for array initialization. int arr[5] = {1, 2, 3, 4, 5}; initializes with specific values. int arr[5]; leaves elements uninitialized.Final Answer:
int arr[5] = {}; -> Option AQuick Check:
Empty braces {} zero-initialize arrays [OK]
- Thinking int arr[5] = 0; initializes the array to zero
- Leaving array uninitialized expecting zeros
- Confusing initialization with assignment
Solution
Step 1: Check array declaration syntax
Arrays require specifying size and initialization with braces {} containing values.Step 2: Validate each option
double arr[3] = {1.1, 2.2, 3.3}; correctly declares a double array of size 3 with values in braces. double arr = {1.1, 2.2, 3.3}; misses size. double arr[3] = (1.1, 2.2, 3.3); uses parentheses which is invalid. double arr[3] = [1.1, 2.2, 3.3]; uses square brackets which is invalid in C++.Final Answer:
double arr[3] = {1.1, 2.2, 3.3}; -> Option CQuick Check:
Use braces {} with size for array initialization [OK]
- Omitting array size in declaration
- Using parentheses or square brackets for initialization
- Assigning values without braces
int arr[4] = {5, 10};
for(int i = 0; i < 4; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Understand partial initialization of arrays
When fewer elements are provided than the array size, remaining elements are zero-initialized.Step 2: Analyze the output loop
arr[0] = 5, arr[1] = 10, arr[2] = 0, arr[3] = 0. Loop prints all four elements separated by spaces.Final Answer:
5 10 0 0 -> Option DQuick Check:
Partial init fills rest with zeros [OK]
- Assuming uninitialized elements have garbage values
- Expecting compilation error for partial init
- Confusing output spacing
int arr[3];
arr = {1, 2, 3};Solution
Step 1: Understand array assignment rules in C++
Arrays cannot be assigned new values after declaration using brace initialization syntax.Step 2: Identify correct way to assign values
Values must be assigned element-wise or initialized at declaration. Direct assignment with braces causes error.Final Answer:
Arrays cannot be assigned values after declaration using braces -> Option AQuick Check:
Arrays only initialized at declaration with braces [OK]
- Trying to assign arrays with brace syntax after declaration
- Confusing arrays with std::vector assignment
- Using parentheses instead of braces
Solution
Step 1: Analyze initialization options
int arr[2][3] = {{7,7,7}}; sets first row to 7,7,7 and second row to 0,0,0. int arr[2][3] = {7}; sets only first element to 7, rest zero. int arr[2][3] = {{7}, {7}}; sets first element of each row to 7, rest zero. int arr[2][3]; for(int i=0; i<2; i++) for(int j=0; j<3; j++) arr[i][j] = 7; uses loops to assign 7 to every element.Step 2: Identify the best approach for uniform value
int arr[2][3]; for(int i=0; i<2; i++) for(int j=0; j<3; j++) arr[i][j] = 7; is the most scalable and clear way to set all elements to 7 regardless of array size.Final Answer:
int arr[2][3]; for(int i=0; i<2; i++) for(int j=0; j<3; j++) arr[i][j] = 7; -> Option BQuick Check:
Use loops to fill multi-dimensional arrays uniformly [OK]
- Assuming partial brace init sets all elements
- Using single brace sets only first element
- Not using loops for uniform initialization
Ready to prove it?
Pass the test and this pattern moves unknown
