Array Traversal Patterns in DSA C++ - Time & Space Complexity
When we look at array traversal, we want to know how long it takes to visit each item in the array.
The question is: how does the time grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
This code visits each element of the array once and prints it.
- Primary operation: The for-loop that visits each element in the array.
- How many times: Exactly once for each element, so n times if the array has n elements.
As the array size grows, the number of steps grows in a straight line with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: Doubling the array size doubles the work done.
Time Complexity: O(n)
This means the time to complete grows directly with the number of elements in the array.
[X] Wrong: "Visiting each element takes the same time no matter how many elements there are."
[OK] Correct: Actually, each element adds a little more time, so more elements mean more total time.
Understanding how simple loops grow with input size helps you explain your code's efficiency clearly and confidently.
"What if we nested another loop inside to compare each element with every other element? How would the time complexity change?"
Practice
arr of size n?for (int i = 0; i < n; i++) { /* code */ }Solution
Step 1: Understand the loop variable and range
The loop variableistarts at 0 and increases by 1 until it reachesn-1.Step 2: Determine the traversal order
Sinceiincrements from 0 ton-1, the loop visits elements from the first to the last in order.Final Answer:
It visits each element from the first to the last in order. -> Option AQuick Check:
Forward traversal = It visits each element from the first to the last in order. [OK]
- Thinking it visits elements in reverse order
- Assuming it skips elements
- Confusing index start with 1 instead of 0
arr of size n backwards in C++?Solution
Step 1: Identify correct start and end conditions
To traverse backwards, start atn-1(last index) and go down to 0.Step 2: Check loop condition and decrement
The loop must continue whilei >= 0and decrementi--each time.Final Answer:
for (int i = n-1; i >= 0; i--) { /* code */ } -> Option AQuick Check:
Backward loop: start=n-1, end=0, decrement [OK]
- Using i <= 0 instead of i >= 0
- Starting at n instead of n-1
- Incrementing instead of decrementing
int arr[] = {10, 20, 30, 40, 50};
int n = 5;
for (int i = 1; i < n; i += 2) {
std::cout << arr[i] << " ";
}Solution
Step 1: Identify loop indices visited
The loop starts at i=1 and increments by 2: i=1, 3, then stops before 5.Step 2: Find elements at these indices
arr[1] = 20, arr[3] = 40. These are printed separated by space.Final Answer:
20 40 -> Option DQuick Check:
Indices 1 and 3 values = 20 40 [OK]
- Including index 5 which is out of bounds
- Starting from 0 instead of 1
- Printing all elements instead of skipping
arr of size n:for (int i = 0; i <= n; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Analyze loop boundary condition
The loop runs whilei <= n, so it accessesarr[n]which is out of bounds.Step 2: Correct the loop condition
Change condition toi < nto access valid indices 0 to n-1.Final Answer:
The loop condition should be i < n instead of i <= n. -> Option BQuick Check:
Array indices 0 to n-1 valid, not n [OK]
- Using <= n causes out-of-bounds access
- Starting loop at 1 misses first element
- Decrementing i in a forward loop
arr of size n, which code snippet correctly prints elements at even indices in reverse order?Solution
Step 1: Determine the highest even index to start
Ifnis even, highest even index isn-2; if odd, it'sn-1.Step 2: Loop backwards by 2 from that index down to 0
The loop decrementsiby 2 each time, printing even indices in reverse order.Final Answer:
for (int i = (n % 2 == 0 ? n-2 : n-1); i >= 0; i -= 2) { std::cout << arr[i] << " "; } -> Option CQuick Check:
Start at highest even index, decrement by 2 [OK]
- Starting at n-1 always (may be odd index)
- Loop condition incorrect (i <= 0 instead of i >= 0)
- Incrementing instead of decrementing in reverse
Ready to prove it?
Pass the test and this pattern moves unknown
