Beginner~4 min · +40 XP
Count Linear Search Comparisons
Practices: Linear Search
Problem
You run linear search for the value 9 on the array [3, 7, 1, 9, 4, 2, 8].
Linear search checks elements left to right and stops the moment it finds the target.
How many comparisons does it make in total?
Examples
in: array [5, 2, 8], target 2
out: 2 comparisons
Index 0 is compared (5 ≠ 2), then index 1 (2 = 2), stop.
Constraints & conventions
- Count every element examined, including the successful match.
Your answer
Solve the challenge to unlock the full approach and complexity analysis.