Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially Last Updated : 08 May, 2019 Comments Improve Suggest changes Like Article Like Report For such cases, time complexity of the loop is O(log(log(n))).The following cases analyse different aspects of the problem. Case 1 : CPP for (int i = 2; i <=n; i = pow(i, k)) { // some O(1) expressions or statements } In this case, i takes values 2, 2k, (2k)k = 2k2, (2k2)k = 2k3, ..., 2klogk(log(n)). The last term must be less than or equal to n, and we have 2klogk(log(n)) = 2log(n) = n, which completely agrees with the value of our last term. So there are in total logk(log(n)) many iterations, and each iteration takes a constant amount of time to run, therefore the total time complexity is O(log(log(n))). Case 2 : CPP // func() is any constant root function for (int i = n; i > 1; i = func(i)) { // some O(1) expressions or statements } In this case, i takes values n, n1/k, (n1/k)1/k = n1/k2, n1/k3, ..., n1/klogk(log(n)), so there are in total logk(log(n)) iterations and each iteration takes time O(1), so the total time complexity is O(log(log(n))). Refer below article for analysis of different types of loops. https://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-loops/ Comment More infoAdvertise with us Next Article Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially R Rishav_rj Improve Article Tags : Misc Analysis of Algorithms DSA Practice Tags : Misc Similar Reads Time Complexity where loop variable is incremented by 1, 2, 3, 4 .. What is the time complexity of below code? C++ #include <iostream> using namespace std; void fun(int n) { int j = 1, i = 0; while (i < n) { // Some O(1) task i = i + j; j++; } } C void fun(int n) { int j = 1, i = 0; while (i < n) { // Some O(1) task i = i + j; j++; } } Java // Method wit 2 min read Time Complexity of Loop with Powers What is the time complexity of the below function?C++void fun(int n, int k) { for (int i = 1; i <= n; i++) { int p = pow(i, k); for (int j = 1; j <= p; j++) { // Some O(1) work } } } // This code is contributed by Shubham SinghCvoid fun(int n, int k) { for (int i = 1; i <= n; i++) { int p = 2 min read What is Logarithmic Time Complexity? A Complete Tutorial Logarithmic time complexity is denoted as O(log n). It is a measure of how the runtime of an algorithm scales as the input size increases. In this comprehensive tutorial. In this article, we will look in-depth into the Logarithmic Complexity. We will also do various comparisons between different log 15+ min read What does Constant Time Complexity or Big O(1) mean? Big O notation is a concept, in computer science and mathematics that allows us to analyse and describe the efficiency of algorithms for worst cases. It provides a way to measure how the runtime of an algorithm or function changes as the input size grows. In this article we'll explore the idea of O( 5 min read A Time Complexity Question What is the time complexity of following function fun()? Assume that log(x) returns log value in base 2. C++ void fun() { int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= log(i); j++) cout << "GeeksforGeeks"; } // This code is contributed by SHUBHAMSINGH10. C void fun() { in 2 min read Like