Multi-dimensional arrays in C++ - Time & Space Complexity
When working with multi-dimensional arrays, it is important to understand how the time to process them grows as their size increases.
We want to know how the number of operations changes when we access or loop through all elements in these arrays.
Analyze the time complexity of the following code snippet.
int matrix[3][4] = {0};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = i + j;
}
}
This code fills a 3 by 4 matrix with the sum of its indices.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop writes to each element of the matrix.
- How many times: The inner loop runs 4 times for each of the 3 outer loop iterations, totaling 12 times.
Imagine the matrix size changes to n rows and m columns. The total operations grow as the product of n and m.
| Input Size (n x m) | Approx. Operations |
|---|---|
| 3 x 4 | 12 |
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
Pattern observation: Doubling both dimensions roughly quadruples the operations because you visit every element.
Time Complexity: O(n * m)
This means the time grows proportionally to the total number of elements in the multi-dimensional array.
[X] Wrong: "The time complexity is O(n) because there is only one loop inside another."
[OK] Correct: Each loop multiplies the work. The inner loop runs completely for every outer loop iteration, so total work is the product, not just one loop's size.
Understanding how nested loops over multi-dimensional arrays affect time helps you explain and optimize code clearly in interviews.
"What if the inner loop size depends on the outer loop index (e.g., j runs from 0 to i)? How would the time complexity change?"
Practice
Solution
Step 1: Understand array dimensions
A multi-dimensional array uses multiple indexes to access elements, such as row and column in a table.Step 2: Compare options
Only An array with more than one index, like rows and columns describes an array with more than one index, matching the concept of multi-dimensional arrays.Final Answer:
An array with more than one index, like rows and columns -> Option DQuick Check:
Multi-dimensional array = multiple indexes [OK]
- Confusing arrays with single values
- Thinking functions return multiple values
- Mixing variables with arrays
Solution
Step 1: Recall C++ 2D array syntax
The correct syntax uses square brackets with sizes: int arr[rows][columns];Step 2: Check each option
int arr[3][4]; uses int arr[3][4]; which means 3 rows and 4 columns, correct syntax. Others are invalid or reversed.Final Answer:
int arr[3][4]; -> Option AQuick Check:
Use square brackets for dimensions [OK]
- Using parentheses instead of brackets
- Swapping row and column sizes
- Using curly braces for declaration
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
std::cout << arr[1][2];Solution
Step 1: Understand array initialization
The array has 2 rows and 3 columns: first row {1,2,3}, second row {4,5,6}.Step 2: Access element arr[1][2]
Row index 1 is second row, column index 2 is third element: value is 6.Final Answer:
6 -> Option AQuick Check:
arr[1][2] = 6 [OK]
- Confusing row and column indexes
- Forgetting zero-based indexing
- Reading wrong element from array
int matrix[2][2] = {{1, 2}, {3}};
std::cout << matrix[1][1];Solution
Step 1: Check array initialization
The second row has only one value {3}, missing one element for 2 columns.Step 2: Understand C++ behavior for missing elements
Missing elements are initialized to 0 automatically, so matrix[1][1] is 0.Step 3: Check output statement
std::cout syntax is correct, so it prints 0 without error.Final Answer:
No error, code runs and prints 0 -> Option CQuick Check:
Missing elements default to 0 in arrays [OK]
- Assuming missing elements cause errors
- Thinking std::cout syntax is wrong
- Expecting garbage values instead of zero
grid in C++?Solution
Step 1: Understand 2D array traversal
To access all elements in a 3x3 array, use two loops: one for rows, one for columns.Step 2: Sum elements with nested loops
Inside inner loop, add each element grid[i][j] to a sum variable to get total sum.Final Answer:
Use nested loops to add each element to a sum variable -> Option BQuick Check:
Nested loops sum all elements [OK]
- Using single loop ignoring 2D structure
- Summing only part of the array
- Trying to use std::accumulate on 2D array directly
Ready to prove it?
Pass the test and this pattern moves unknown
