# Time Complexity of Loop with Powers

What is the time complexity of the below function?

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

## 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` ` ` `}` ` ` `}` `}` |

Time complexity of above function can be written as 1^{k} + 2^{k} + 3^{k} + … n1^{k}.

Let us try few examples:

k=1 Sum = 1 + 2 + 3 ... n = n(n+1)/2 = n^{2}/2 + n/2 k=2 Sum = 1^{2}+ 2^{2}+ 3^{2}+ ... n1^{2}. = n(n+1)(2n+1)/6 = n^{3}/3 + n^{2}/2 + n/6 k=3 Sum = 1^{3}+ 2^{3}+ 3^{3}+ ... n1^{3}. = n^{2}(n+1)^{2}/4 = n^{4}/4 + n^{3}/2 + n^{2}/4

In general, asymptotic value can be written as **(n ^{k+1})/(k+1) + Θ(n^{k})**

If n>=k then the time complexity will be considered in

**O((n**and if n<k, then the time complexity will be considered as in the

^{k+1})/(k+1))**O(**

**n**^{k})Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above