What is the time complexity of below code?
The loop variable ‘i’ is incremented by 1, 2, 3, 4, … until i becomes greater than or equal to n.
The value of i is x(x+1)/2 after x iterations. So if loop runs x times, then x(x+1)/2 < n. Therefore time complexity can be written as Θ(√n).
This article is contributed by Piyush Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Time Complexity of Loop with Powers
- Time taken by Loop unrolling vs Normal loop
- Queries of nCr%p in O(1) time complexity
- A Time Complexity Question
- An interesting time complexity question
- Time Complexity of building a heap
- Time complexity of all permutations of a string
- Sieve of Eratosthenes in 0(n) time complexity
- Practice Questions on Time Complexity Analysis
- GATE CS 2016 Sec 5 – Time Space Complexity
- An Insertion Sort time complexity question
- Time complexity of insertion sort when there are O(n) inversions?
- Extended Mo's Algorithm with ≈ O(1) time complexity
- Time complexity of recursive Fibonacci program