Reputation: 11
Below are two ways I could traverse any array:
How would the time complexity vary, would it be reduced in second case or it would be same?
Upvotes: 0
Views: 3286
Reputation: 1377
Of course the same. Both are O(n), in fact there is no way to traverse an array faster than O(n).Even if you tarverse from opposite direction, you still have to visit each element once.
Upvotes: 1