Professional Documents
Culture Documents
es
{ 3T(n-1), if n>0,
T(n) = { 1, otherwise
Solution:
{ 2T(n-1) - 1, if n>0,
T(n) = { 1, otherwise
Solution:
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/ 1/8
1/28/2020 Analysis of Algorithms | Set 5 (Practice Problems) - GeeksforGeeks
2 1 0
..... 2 - 2 - 2
function(int n)
{
if (n==1)
return;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
printf("*");
break;
}
}
}
function(int n)
{
if (n==1)
return;
for (int i=1; i<=n; i++)
{
// Inner loop executes only one
// time due to break statement.
for (int j=1; j<=n; j++)
{
printf("*");
break;
}
}
}
Time Complexity of the above function O(n). Even though the inner loop is bounded by n, but due to
break statement it is executing only once.
▲
Problem 4: Find the complexity of the below program:
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/ 2/8
1/28/2020 Analysis of Algorithms | Set 5 (Practice Problems) - GeeksforGeeks
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j<=n; j = 2 * j)
for (int k=1; k<=n; k = k * 2)
count++;
}
void function(int n)
{
⇣
int count = 0;
for (int i=n/2; i<=n; i++)
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j+n/2<=n; j = j++)
for (int k=1; k<=n; k = k * 2)
count++;
}
void function(int n)
{
int count = 0;
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/ 3/8
1/28/2020 Analysis of Algorithms | Set 5 (Practice Problems) - GeeksforGeeks
void function(int n)
{
int i = 1, s =1;
while (s <= n)
{
i++;
s += i;
printf("*");
⇣
}
}
Solution: We can de ne the terms ‘s’ according to relation si = si-1 + i. The value of ‘i’ increases by one
for each iteration. The value contained in ‘s’ at the ith iteration is the sum of the rst ‘i’ positive integers.
If k is total number of iterations taken by the program, then while loop terminates if: 1 + 2 + 3 ….+ k =
[k(k+1)/2] > n So k = O(√n).
void function(int n)
{
int count = 0;
for (int i=0; i<n; i++)
for (int j=i; j< i*i; j++)
if (j%i == 0)
{
for (int k=0; k<j; k++)
printf("*");
}
}
void function(int n)
{
int count = 0;
// executes n times
for (int i=0; i<n; i++)
}
}
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to
contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help
other Geeks.
Please write comments if you nd anything incorrect, or you want to share more information about the
topic discussed above.
⇣
Recommended Posts:
Practice Questions on Time Complexity Analysis
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Analysis of Algorithms | Set 4 (Analysis of Loops)
Analysis of Algorithms | Big-O analysis
Analysis of Algorithms | Set 3 (Asymptotic Notations)
Analysis of algorithms | little o and little omega notations
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
Asymptotic Analysis and comparison of sorting algorithms
Algorithms Sample Questions | Set 3 | Time Order Analysis
Analysis of Algorithm | Set 5 (Amortized Analysis Introduction)
Analysis of different sorting techniques
Practice Set for Recurrence Relations
Analysis of Algorithm | Set 4 (Solving Recurrences)
Amortized analysis for increment in counter
Complexity Analysis of Binary Search
16
3.2
To-do Done
Based on 18 vote(s)
▲
Feedback/ Suggest Improvement Add Notes Improve Article
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/ 5/8
1/28/2020 Analysis of Algorithms | Set 5 (Practice Problems) - GeeksforGeeks
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
13 Comments GeeksforGeeks
1 Login
LOG IN WITH
Name
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/ 7/8
1/28/2020 Analysis of Algorithms | Set 5 (Practice Problems) - GeeksforGeeks
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
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/ 8/8