What is the time complexity of below code?

`void` `fun(` `int` `n) ` `{ ` ` ` `int` `j = 1, i = 0; ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` `// Some O(1) task ` ` ` `i = i + j; ` ` ` `j++; ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

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

## Recommended Posts:

- 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
- A Time Complexity Question
- An interesting time complexity question
- Time Complexity of building a heap
- Practice Questions on Time Complexity Analysis
- Understanding Time Complexity with Simple Examples
- Time complexity of recursive Fibonacci program
- Time Complexity Analysis | Tower Of Hanoi (Recursion)
- Python Code for time Complexity plot of Heap Sort
- What does 'Space Complexity' mean?
- Cyclomatic Complexity
- Knowing the complexity in competitive programming
- Interesting Infinite loop using characters in C