Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
I am confused why the algorithm is O(N) running time if there is a for loop and ...

Keila Mohan

Jan 21, 2023

I am confused why the algorithm is O(N) running time if there is a for loop and a while loop inside.

0

1

Comments
Design Gurus
Design Gurus3 years ago

This is similar to the previous problem, 'Longest substring with..".

The difficult thing to understand is the inner 'while' loop.

The 'while' runs a total of 'n' times for ALL iterations of the 'for' loop. That is why it is O(n+n) = O(n), as explained in the analysis...

C
chunhung.yeung1130 a year ago

The inner while loop never goes backward, only forward.