0% found this document useful (1 vote)
204 views

Time Complexity

The document discusses the time complexity of a function that contains two nested loops. The outer loop iterates from 1 to n, and the inner loop iterates from 1 to a power of the outer loop counter i, which is raised to the power of k. The time complexity is analyzed for different values of k. In general, the asymptotic time complexity is Θ(nk+1/(k+1)), ignoring lower order terms. Some examples are provided to illustrate how to calculate the time complexity for specific values of k.

Uploaded by

Ashly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
204 views

Time Complexity

The document discusses the time complexity of a function that contains two nested loops. The outer loop iterates from 1 to n, and the inner loop iterates from 1 to a power of the outer loop counter i, which is raised to the power of k. The time complexity is analyzed for different values of k. In general, the asymptotic time complexity is Θ(nk+1/(k+1)), ignoring lower order terms. Some examples are provided to illustrate how to calculate the time complexity for specific values of k.

Uploaded by

Ashly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6/20/2019 Time Complexity of Loop with Powers - GeeksforGeeks

Custom Search

COURSES

HIRE WITH US


Time Complexity of Loop with Powers
What is the time complexity of below function?

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 1k + 2k + 3k + … n1k.

Let us try few examples:

k=1
Sum = 1 + 2 + 3 ... n
= n(n+1)/2
= n2 + n/2

k=2
Sum = 12 + 22 + 32 + ... n12.
= n(n+1)(2n+1)/6
= n3/3 + n2/2 + n/6

k=3
Sum = 13 + 23 + 33 + ... n13.
= n2(n+1)2/4
= n4/4 + n3/2 + n2/4

https://www.geeksforgeeks.org/time-complexity-of-loop-with-powers/ 1/3
6/20/2019 Time Complexity of Loop with Powers - GeeksforGeeks

In general, asymptotic value can be written as (nk+1)/(k+1) + Θ(nk)

Note that, in asymptotic notations like Θ we can always ignore lower order terms. So the time
complexity is Θ(nk+1 / (k+1))

Please write comments if you nd 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 where loop variable is incremented by 1, 2, 3, 4 ..
Time taken by Loop unrolling vs Normal loop
A Time Complexity Question
An interesting time complexity question
Time Complexity of building a heap
Time complexity of recursive Fibonacci program
Understanding Time Complexity with Simple Examples
Practice Questions on Time Complexity Analysis
Time Complexity Analysis | Tower Of Hanoi (Recursion)
Python Code for time Complexity plot of Heap Sort
What does 'Space Complexity' mean?
Cyclomatic Complexity
Complexity Analysis of Binary Search
Knowing the complexity in competitive programming

Article Tags : Analysis Articles time complexity


8

https://www.geeksforgeeks.org/time-complexity-of-loop-with-powers/ 2/3
6/20/2019 Time Complexity of Loop with Powers - GeeksforGeeks

To-do Done 2.6

Based on 83 vote(s)

Feedback/ Suggest Improvement Notes Improve Article

Please write to us at [email protected] to report any issue with the above content.

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305
[email protected]

COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

PRACTICE CONTRIBUTE
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks, Some rights reserved

https://www.geeksforgeeks.org/time-complexity-of-loop-with-powers/ 3/3

You might also like