Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Big O Time Complexity of While Loops

I am a bit confused about the time complexity, in the case of two seperate while loops.
I am aware that the code:

while(i<N){
     code
    while(k<N){
       code
    }
}

will have a complexity of O(n^2)
What about the case where we don’t have nested loops, though?

while(i<N){
}
while(k<N){
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

So you run two loops, one after the other. If they both perform n iterations, then your code performs 2n loop iterations in total.

Now, 2n = O(2n) = O(n) (by the properties of big-O notation), so that is your time complexity.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading