Array Reversal Techniques in DSA C - Time & Space Complexity
We want to understand how the time needed to reverse an array changes as the array gets bigger.
How does the number of steps grow when the array size grows?
Analyze the time complexity of the following code snippet.
void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
This code reverses the elements of an array by swapping pairs from the start and end moving towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Swapping two elements inside a while loop.
- How many times: The loop runs about n/2 times, where n is the array size.
As the array size grows, the number of swaps grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 swaps |
| 100 | 50 swaps |
| 1000 | 500 swaps |
Pattern observation: The number of operations grows linearly with the input size.
Time Complexity: O(n)
This means the time to reverse the array grows directly in proportion to the array size.
[X] Wrong: "Reversing an array takes constant time because we just swap elements."
[OK] Correct: Even though each swap is quick, the number of swaps grows with the array size, so total time grows too.
Knowing how array reversal scales helps you understand basic array operations and prepares you for more complex problems.
"What if we used recursion to reverse the array instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand array order
An array has elements arranged from first to last index.Step 2: Define reversal
Reversing means swapping elements so the last element moves to the first position and vice versa.Final Answer:
Changing the order of elements so the last becomes first and first becomes last -> Option AQuick Check:
Reversal = Reverse order [OK]
- Confusing reversal with sorting
- Thinking reversal removes elements
- Assuming reversal adds elements
arr[i] and arr[j] for array reversal?Solution
Step 1: Use a temporary variable for swapping
To swap two values, store one in a temporary variable to avoid overwriting.Step 2: Assign values correctly
Assignarr[i]to temp, thenarr[j]toarr[i], then temp toarr[j].Final Answer:
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option CQuick Check:
Swap uses temp variable [OK]
- Overwriting one element before saving it
- Using uninitialized temp variable
- Swapping without temp causes data loss
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
for(int i = 0; i < n/2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}Solution
Step 1: Understand the loop swaps
The loop swaps elements from start and end moving towards the center: arr[0] with arr[4], arr[1] with arr[3].Step 2: Trace swaps
Swap 1 and 5 -> arr = {5, 2, 3, 4, 1}; Swap 2 and 4 -> arr = {5, 4, 3, 2, 1}; middle element 3 stays same.Final Answer:
5 4 3 2 1 -> Option BQuick Check:
Reversed array = 5 4 3 2 1 [OK]
- Not swapping all pairs correctly
- Loop running too many or too few times
- Confusing indices in swap
int arr[] = {10, 20, 30, 40};
int n = 4;
for(int i = 0; i <= n/2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}Solution
Step 1: Analyze loop condition
The loop runs from i = 0 to i <= n/2, which means it runs one extra iteration when i = n/2.Step 2: Understand effect of extra iteration
For even n=4, n/2=2, so i=2 is invalid index for swapping (out of bounds or swaps middle element twice).Final Answer:
The loop condition should bei < n/2instead ofi <= n/2-> Option DQuick Check:
Loop runs too far, fix condition [OK]
i < n/2 to avoid extra swap [OK]- Using <= causes out-of-bound swaps
- Confusing array length with index
- Ignoring zero-based indexing
arr of size n, which approach is best to reverse the array in-place with minimum extra memory and why?Solution
Step 1: Compare memory usage of methods
Creating a new array uses extra memory equal to array size; recursion uses call stack memory.Step 2: Evaluate two-pointer method
Two pointers swap elements in-place without extra memory, making it efficient and simple.Final Answer:
Use two pointers, one at start and one at end, swap and move towards center -> Option AQuick Check:
Two-pointer swap = minimal memory [OK]
- Using extra arrays wastes memory
- Recursion adds unnecessary stack overhead
- Sorting does not reverse original order
Ready to prove it?
Pass the test and this pattern moves unknown
