Problem
This loop runs until an even number is found. What is the time complexity?
def find_even(arr):
for i in arr:
if i % 2 == 0:
return i
Solution
We're only interested in the upper bound. We can expect the following runtime if the loop never finds an even number:
Input Size | Steps |
---|---|
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
How does the runtime increase w.r.t. input size?
The number of steps is determined by the size of the array .
What is the time complexity?
: the runtime increases linearly with the input size.
This workbook was created by Jad and Rayan Slim. Feel free to explore some of their courses:
The Complete Java Development Bootcamp
The Complete Spring Boot Development Bootcamp – Become a Java Web Developer
Feedback Summary
0.0
0 students
5
0%
4
0%
3
0%
2
0%
1
0%
Written Reviews
There are no written reviews yet.