Time Complexity of Loop with Powers Last Updated : 23 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Singh 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 } } } Java static void fun(int n, int k) { for (int i = 1; i <= n; i++) { int p = Math.pow(i, k); for (int j = 1; j <= p; j++) { // Some O(1) work } } } // This code is contributed by umadevi9616 Python def fun(n, k): for i in range(1, n + 1): p = pow(i, k) for j in range(1, p + 1): # Some O(1) work # This code is contributed by Shubham Singh C# static void fun(int n, int k) { for (int i = 1; i <= n; i++) { int p = Math.Pow(i, k); for (int j = 1; j <= p; j++) { // Some O(1) work } } } // This code is contributed by umadevi9616 JavaScript <script> // JavaScript program for the above approach function fun(n, k) { for(let i = 1; i <= n; i++) { int p = Math.pow(i, k); for (let j = 1; j <= p; j++) { // Some O(1) work } } } // This code is contributed by Shubham Singh </script> Time complexity of above function can be written as 1k + 2k + 3k + ... n1k.Let us try few examples: k=1Sum = 1 + 2 + 3 ... n = n(n+1)/2 = n2/2 + n/2k=2Sum = 12 + 22 + 32 + ... n12. = n(n+1)(2n+1)/6 = n3/3 + n2/2 + n/6k=3Sum = 13 + 23 + 33 + ... n13. = n2(n+1)2/4 = n4/4 + n3/2 + n2/4 In general, asymptotic value can be written as (nk+1)/(k+1) + Θ(nk)If n>=k then the time complexity will be considered in O((nk+1)/(k+1)) and if n<k, then the time complexity will be considered as in the O(nk) Comment More infoAdvertise with us Next Article Time Complexity of a Loop when Loop variable âExpands or Shrinksâ exponentially K kartik Follow Improve Article Tags : Analysis of Algorithms DSA time complexity Similar Reads 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 Multiplication with a power of 2 Given two numbers x and n, we need to multiply x with 2nExamples : Input : x = 25, n = 3 Output : 200 25 multiplied by 2 raised to power 3 is 200. Input : x = 70, n = 2 Output : 280 A simple solution is to compute n-th power of 2 and then multiply with x. C++ // Simple C/C++ program // to compute x 5 min read Compute power of power k times % m Given x, k and m. Compute (xxxx...k)%m, x is in power k times. Given x is always prime and m is greater than x. Examples: Input : 2 3 3 Output : 1 Explanation : ((2 ^ 2) ^ 2) % 3 = (4 ^ 2) % 3 = 1 Input : 3 2 3 Output : 0 Explanation : (3^3)%3 = 0 A naive approach is to compute the power of x k time 15+ 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 Time Complexity of a Loop when Loop variable âExpands or Shrinksâ exponentially 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( 1 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 Like