Mastering Array Traversal Patterns
Arrays are not just for storing values.
The real power comes when we process all elements, and that is where traversal becomes important.
What Is Traversal?
Traversal means visiting each element in an array one by one.
Think of it like:
- Taking attendance in a classroom
- Checking each student one after another
You cannot skip anyone. You must go through every element.
Why Traversal Is Important?
Most array problems require you to:
- Add all values (sum)
- Find the average (mean)
- Find the smallest value (min)
- Find the largest value (max)
- Count elements based on a condition
All of these require checking every element.
Basic Traversal (Index-Based)
Let us start with an example:
arr = [5, 10, 15]
for i in range(len(arr)):
print(arr[i])
Step-by-step execution
i = 0->arr[0]->5i = 1->arr[1]->10i = 2->arr[2]->15
Output:
5
10
15
Better and Cleaner Style (Recommended)
Instead of using index, Python gives a simpler way:
for num in arr:
print(num)
Here, num directly takes each value from the array.
Why this is better:
- Easier to read
- Less chance of mistakes
- Cleaner code
Time Complexity of Traversal
Traversal always takes O(n) because each element is visited once.
Pattern 1: Sum of Array Elements
Example:
marks = [80, 75, 90, 85]
Step 1: Initialize a variable
total = 0
This variable stores the running total.
Step 2: Traverse and update
for m in marks:
total += m
Step-by-step flow
Start -> total = 0
0 + 80 = 80
80 + 75 = 155
155 + 90 = 245
245 + 85 = 330
Step 3: Print result
print(total)
Output:
330
Time complexity: O(n)
Pattern 2: Finding Minimum and Maximum
Example:
arr = [300, 150, 450, 200]
Step 1: Initialize
min_val = arr[0]
max_val = arr[0]
We use the first value as the starting point.
Step 2: Traverse and compare
for x in arr:
if x < min_val:
min_val = x
if x > max_val:
max_val = x
Step-by-step thinking
Start: min = 300, max = 300
150 -> update min
450 -> update max
200 -> no change
Final: min = 150, max = 450
Step 3: Print result
print(min_val, max_val)
Time complexity: O(n)
Pattern 3: Average (Mean)
The average (also called mean) answers:
“What is the typical value if everything is shared equally?”
For numbers in an array, we use:
average = sum of all elements / number of elements
Example:
scores = [80, 75, 90, 85]
Step 1: Find the total (same as Pattern 1)
total = 0
for s in scores:
total += s
Here total becomes 330.
Step 2: Divide by how many elements we have
n = len(scores)
average = total / n
print(average)
Output:
82.5
Why traversal helps
- We still visit every element once to get the sum (O(n)).
- Division by
nis O(1) after the sum is ready.
So the whole process stays O(n).
One-line style (when you already know Python helpers)
average = sum(scores) / len(scores)
Use the loop version when you want to show each step clearly in an interview or exam.
Core Idea: One-Pass Thinking
Whenever you see problems like:
- sum
- average (after you have sum and count)
- min / max
- count
Use this pattern:
- Create a variable.
- Traverse the array once.
- Update the variable for each element.
Real-Life Analogy
Think of counting money in your wallet:
- You take each note one by one
- Add it to total
- Finish in one pass
That is exactly how traversal works.
Final Takeaways
- Traversal means visiting each element one by one.
- Most array operations depend on traversal.
- Prefer
for num in arrfor cleaner Python code. - Sum, average, min, and max follow the same building blocks (sum needs one pass; average adds one division).
- Traversal time complexity is usually O(n).
