Array Reversal Techniques in DSA Java - Time & Space Complexity
We want to understand how the time needed to reverse an array changes as the array gets bigger.
Specifically, how many steps does it take to reverse all elements?
Analyze the time complexity of the following code snippet.
public void reverseArray(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
This code reverses the elements of an array by swapping pairs from the ends moving toward the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Swapping two elements in the array.
- How many times: Approximately half the length of the array (n/2 times).
As the array size grows, the number of swaps grows roughly half as fast.
| Input Size (n) | Approx. Operations (Swaps) |
|---|---|
| 10 | 5 |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: The number of operations grows linearly with input size.
Time Complexity: O(n)
This means the time to reverse the array grows directly in proportion to the number of elements.
[X] Wrong: "Reversing an array takes n² steps because of swapping elements inside a loop."
[OK] Correct: Each element is swapped only once, so the total steps grow linearly, not quadratically.
Knowing how to analyze simple loops like array reversal helps build confidence for more complex problems.
"What if we used recursion to reverse the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand array reversal meaning
Reversing means flipping the order of elements so the last element comes first and the first element comes last.Step 2: Compare with other options
Sorting arranges elements by value, removing duplicates deletes repeated items, and adding elements is insertion, not reversal.Final Answer:
Changing the order of elements so the last becomes first and first becomes last -> Option AQuick Check:
Reversal = Flip order [OK]
- Confusing reversal with sorting
- Thinking reversal removes elements
- Mixing reversal with adding elements
arr in-place?Solution
Step 1: Analyze in-place reversal logic
for(int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } swaps elements from start and end moving towards center, correctly reversing the array in-place.Step 2: Check other options
for(int i = 0; i < arr.length; i++) { arr[i] = arr[arr.length - 1 - i]; } overwrites elements incorrectly, for(int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i]); } only prints reversed array without changing it, Arrays.sort(arr); sorts array, not reverses.Final Answer:
for(int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } -> Option CQuick Check:
Swap pairs from ends = reversal [OK]
- Overwriting elements without swapping
- Only printing reversed array without changing it
- Using sort instead of reversal
int[] arr = {1, 2, 3, 4, 5};
for(int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
for(int num : arr) {
System.out.print(num + " ");
}Solution
Step 1: Trace the reversal loop
The loop swaps arr[0] with arr[4], arr[1] with arr[3]. After swaps, array becomes [5,4,3,2,1].Step 2: Print the array elements
The print loop outputs elements in order: 5 4 3 2 1Final Answer:
5 4 3 2 1 -> Option AQuick Check:
Swapped ends = reversed array [OK]
- Not swapping correctly, leaving array unchanged
- Confusing print output with original array
- Off-by-one errors in loop
arr:for(int i = 0; i <= arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}Solution
Step 1: Analyze loop condition
The loop uses 'i <= arr.length / 2', which causes the middle element to be swapped twice when array length is odd.Step 2: Correct loop condition
Changing condition to 'i < arr.length / 2' ensures swaps happen only once per pair, avoiding double swap of middle element.Final Answer:
The loop condition should be 'i < arr.length / 2' to avoid swapping middle element twice -> Option BQuick Check:
Loop limit excludes middle for odd length [OK]
- Using <= instead of < in loop condition
- Declaring temp outside loop unnecessarily
- Incorrect swap logic swapping wrong elements
matrix of size n x n, which code snippet correctly reverses each row of the matrix in-place?Solution
Step 1: Understand reversing each row
Reversing each row means swapping elements in each row from start to end moving towards center.Step 2: Analyze options
for(int i = 0; i < n; i++) { for(int j = 0; j < n / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } swaps elements in each row correctly. for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { matrix[i][j] = matrix[i][n - 1 - j]; } } overwrites elements incorrectly. for(int j = 0; j < n; j++) { for(int i = 0; i < n / 2; i++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - i][j]; matrix[n - 1 - i][j] = temp; } } swaps columns, not rows. for(int i = 0; i < n; i++) { Arrays.sort(matrix[i]); } sorts rows, not reverses.Final Answer:
for(int i = 0; i < n; i++) { for(int j = 0; j < n / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } -> Option DQuick Check:
Swap pairs in each row for reversal [OK]
- Swapping columns instead of rows
- Overwriting elements without swapping
- Sorting rows instead of reversing
Ready to prove it?
Pass the test and this pattern moves unknown
