Two-dimensional arrays in Java - Time & Space Complexity
When working with two-dimensional arrays, it is important to understand how the time to process them grows as the array size increases.
We want to know how the number of operations changes when we loop through all elements in a 2D array.
Analyze the time complexity of the following code snippet.
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = i + j;
}
}
This code fills every element in a two-dimensional array with the sum of its row and column indices.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Assigning a value to each element in the 2D array.
- How many times: The inner loop runs m times for each of the n iterations of the outer loop, so n x m times in total.
As the number of rows (n) and columns (m) increase, the total operations grow by multiplying these two sizes.
| Input Size (n x m) | Approx. Operations |
|---|---|
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
| 1000 x 1000 | 1,000,000 |
Pattern observation: Doubling both dimensions causes the operations to increase by four times, showing a growth proportional to the product of n and m.
Time Complexity: O(n * m)
This means the time to complete the task grows proportionally to the total number of elements in the two-dimensional array.
[X] Wrong: "The time complexity is O(n) because there is an outer loop running n times."
[OK] Correct: The inner loop also runs m times for each outer loop iteration, so the total work depends on both n and m multiplied together, not just n.
Understanding how nested loops over two-dimensional arrays affect time helps you explain and reason about performance clearly in interviews.
"What if the inner loop only ran up to a fixed number instead of m? How would the time complexity change?"
Practice
Solution
Step 1: Understand the structure of two-dimensional arrays
Two-dimensional arrays store data in rows and columns, like a grid or table.Step 2: Compare options with the definition
Only A table with rows and columns correctly describes this as a table with rows and columns.Final Answer:
A table with rows and columns -> Option CQuick Check:
Two-dimensional array = table [OK]
- Confusing with one-dimensional arrays
- Thinking it's a list of unrelated variables
- Mixing arrays with methods
Solution
Step 1: Recall Java syntax for 2D array declaration
Java uses int[][] name = new int[rows][columns]; to declare 2D arrays.Step 2: Check each option
int[][] matrix = new int[3][4]; matches correct syntax. int matrix[3][4]; is C-style, invalid in Java. int matrix = new int[3,4]; uses comma, invalid. int[] matrix = new int[3][4]; declares a 1D array incorrectly.Final Answer:
int[][] matrix = new int[3][4]; -> Option BQuick Check:
Correct 2D array syntax = int[][] matrix = new int[3][4]; [OK]
- Using commas instead of separate brackets
- Trying C-style declarations
- Mixing 1D and 2D array syntax
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(arr[1][0]);Solution
Step 1: Identify the element at arr[1][0]
arr[1] is the second row: {3, 4}. The element at index 0 in this row is 3.Step 2: Confirm the printed value
System.out.println prints arr[1][0], which is 3.Final Answer:
3 -> Option AQuick Check:
arr[1][0] = 3 [OK]
- Confusing row and column indices
- Starting count at 1 instead of 0
- Mixing up elements in nested arrays
int[][] grid = new int[2][3]; grid[2][1] = 5;
Solution
Step 1: Check array size and indices used
Array grid has 2 rows (indices 0 and 1) and 3 columns (indices 0 to 2). Accessing grid[2][1] uses row index 2 which is out of bounds.Step 2: Identify the error type
Accessing an invalid index causes ArrayIndexOutOfBoundsException at runtime.Final Answer:
Array index out of bounds error -> Option AQuick Check:
Index 2 invalid for 2-row array [OK]
- Using indices equal to array length
- Confusing rows and columns sizes
- Assuming arrays auto-expand
int[][] data = {{1,2},{3,4},{5,6}};, which code snippet correctly calculates the sum of all elements?Solution
Step 1: Understand array dimensions and iteration
data.length is number of rows (3). data[i].length is number of columns (2). Nested loops must iterate rows then columns.Step 2: Analyze each option's loop structure
int sum = 0; for(int i=0; iFinal Answer:
The nested loops with outer loop on data.length and inner on data[i].length -> Option DQuick Check:
Correct nested loops sum all elements [OK]
- Swapping row and column loops
- Adding arrays directly to int
- Using wrong indices in loops
Ready to prove it?
Pass the test and this pattern moves unknown
