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 Raj 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 An interesting time complexity question What is the time complexity of following function fun()? C int fun(int n) { for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j += i) { // Some O(1) task } } } For i = 1, the inner loop is executed n times. For i = 2, the inner loop is executed approximately n/2 times. For i = 3, the inner 2 min read Multiplying a variable with a constant without using multiplication operator As we know that every number can be represented as sum(or difference) of powers of 2, therefore what we can do is represent the constant as a sum of powers of 2.For this purpose we can use the bitwise left shift operator. When a number is bitwise left shifted it is multiplied by 2 for every bit shif 4 min read Miscellaneous Problems of Time Complexity Prerequisite : Asymptotic Notations Time Complexity :Time complexity is the time needed by an algorithm expressed as a function of the size of a problem. It can also be defined as the amount of computer time it needs to run a program to completion. When we solve a problem of time complexity then thi 15 min read Step Count Method for Time Complexity Analysis What is Time Complexity? Time Complexity is the amount of time taken by the algorithm to run. It measures the time taken to execute each statement of code in an algorithm. Time Complexity can be calculated by using Two types of methods. They are: Step Count MethodAsymptotic Notation. Here, we will d 4 min read Practice Questions on Time Complexity Analysis Prerequisite: Analysis of Algorithms1. What is the time, and space complexity of the following code: CPPint a = 0, b = 0; for (i = 0; i < N; i++) { a = a + rand(); } for (j = 0; j < M; j++) { b = b + rand(); } Javaint a = 0, b = 0; for (i = 0; i < N; i++) { a = a + Math.random(); } for (j = 7 min read Like