Below are two ways I could traverse any array:
- Using for loop a variable would traverse from starting to end of array.
- Using while loop 2 variables would traverse from opposite direction and meet in between.
How would the time complexity vary, would it be reduced in second case or it would be same?
O(n/2)(not taking into account rounding it toO(n), of course)O(1)algorithm that always takes exactly 1,000,000 years on today's CPUs and and another algorithm for the same problem that runs inO(n^2)time (but which runs in only a few milliseconds due to some CPU optimization that makes each operation very cheap, but still needs to runn^2times) then that doesn't change their time-complexity and the million-year algorithm is still less-complex in time.