Professional Documents
Culture Documents
Complexity
Complexity
Complexity
90% Refund @Courses DSA Data Structure Analysis of Algorithms Sorting Searching Greedy Recur
{ 3T(n-1), if n>0,
T(n) = { 1, otherwise
Solution:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 1/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
T(n) = 3T(n-1)
= 3(3T(n-2))
= 32T(n-2)
= 33T(n-3)
…
…
= 3nT(n-n)
= 3nT(0)
= 3n
{ 2T(n-1) – 1, if n>0,
T(n) = { 1, otherwise
Solution:
T(n) = 2T(n-1) – 1
= 2(2T(n-2)-1)-1
= 22(T(n-2)) – 2 – 1
We use cookies to ensure you have the best browsing experience on our website. By using
= 22(2T(n-3)-1)
our site, you acknowledge that you have– read
2 –and
1 understood our Cookie Policy & Privacy
= 23T(n-3) – 22 – 2Policy 1 – 20
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 2/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
…..
…..
= 2nT(n-n) – 2n-1 – 2n-2 – 2n-3
….. 22 – 21 – 20
T(n) = 1
Time Complexity is O(1). Note that while the recurrence relation looks
exponential
he solution to the recurrence relation here gives a different result.
CPP
void function(int n)
{
if (n==1)
return;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
cout << "*";
break;
}
cout << endl;
}
}
Java
We usestatic
cookies tovoid
ensurefunction(int
you have the best browsing
n)
experience on our website. By using
our site,
{
you acknowledge that you have read and understood our Cookie Policy & Privacy
if (n==1)
Policy
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 3/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
return;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
System.out.print("*");
break;
}
System.out.println();
}
}
// The code is contributed by Nidhi goel.
Python3
def funct(n):
if (n==1):
return
for i in range(1, n+1):
for j in range(1, n + 1):
print("*", end = "")
break
print()
C#
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 4/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
Javascript
function funct(n)
{
if (n==1)
return;
for (let i=1; i<=n; i++)
{
for (let j=1; j<=n; j++)
{
console.log("*");
break;
}
console.log();
}
}
// The code is contributed by Nidhi goel.
CPP
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;
}
}
}
Java
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 5/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
Python3
def my_function(n):
if n == 1:
return
C#
using System;
class Program
{
static void PrintPattern(int n)
{
if (n == 1)
return;
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 6/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
}
#pragma warning restore CS0162
}
}
Javascript
function printStars(n) {
if (n === 1) {
return; // If n is 1, exit the function
}
// Example usage:
printStars(5); // Call the function with n = 5
Time Complexity: O(n), Even though the inner loop is bounded by n, but due
to the break statement, it is executing only once.
CPP
void function(int n)
{
int count = 0;
We use cookies
forto ensure
(int you have thei<=n;
i=n/2; best browsing
i++) experience on our website. By using
our site, you acknowledge
for (int j=1; j<=n; junderstood
that you have read and = 2 * j) our Cookie Policy & Privacy
for (int k=1; Policyk<=n; k = k * 2)
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 7/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
count++;
}
Java
C#
Javascript
function function1(n)
{
var count = 0;
for (i = n / 2; i <= n; i++)
for (j = 1; j <= n; j = 2 * j)
for (k = 1; k <= n; k = k * 2)
count++;
}
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 8/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
CPP
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
CPP
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++;
}
Java
We use//cookies
Thisto ensure
code you
is have the best browsing
contributed by experience
PushpeshonRaj.
our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 9/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
Javascript
function myFunction(n) {
let count = 0;
return count;
}
CPP
void function(int n)
{
int count = 0;
We useJava
cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
static void function(int Policy
n)
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 10/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
{
int count = 0;
Python3
def function(n):
count = 0
C#
using System;
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 11/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
count++;
}
Javascript
function function(n)
{
let count = 0;
CPP
void function(int n)
{
int i = 1, s =1;
while (s <= n)
{
i++;
s += i;
printf("*");
}
}
We use cookies to ensure you have the best browsing experience on our website. By using
Solution:
our site, We can
you acknowledge define
that you theand
have read terms ‘s’ according
understood to relation
our Cookie Policy & Privacy si = si-1 + i. The
value of ‘i’ increases by one Policy
for each iteration. The value contained in ‘s’ at
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 12/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
the ith iteration is the sum of the first ‘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).
Time Complexity: O(√n).
CPP
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("*");
}
}
CPP
void function(int n)
{
int count = 0;
// executes n times
for (int i=0; i<n; i++)
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 13/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
Feeling lost in the world of random DSA topics, wasting time without
progress? It's time for a change! Join our DSA course, where we'll guide you
on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course,
trusted by over 100,000 geeks!
DSA in C++
DSA in Java
DSA in Python
DSA in JavaScript
Recommended Problems
Solve Problems
Frequently asked DSA Problems
Participate in Three 90 Challenge! Enroll in any GeeksforGeeks course and get 90%
refund by completing 90% course. Explore offer now.
Previous Next
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 14/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
Similar Reads
Asymptotic Notation and Analysis Practice Questions on Time Complexity
(Based on input size) in Complexity Analysis
Analysis of Algorithms
Complete Tutorials
Learn Algorithms with Javascript | DSA DSA Crash Course | Revision Checklist
using JavaScript Tutorial with Interview Guide
WeArticle Tags
use cookies : Scala-Arrays
to ensure you have the ,best
SQL-PL/SQL , TCS-coding-questions
browsing experience on our website. By,using
Tek Systems ,
our site, you acknowledgeAnalysis
that youofhave
Algorithms
read and,understood
DSA our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 15/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
Additional Information
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
We use cookies to ensure you have the best browsing experience on our website. By using
C++ DSA for Beginners
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
PHP Policy Basic DSA Problems
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 16/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
System Design
We use cookies to ensure you have the best browsing experience on our website. By using
JavaScript
our site, you acknowledge
Highthat youDesign
Level have read and understood our Cookie Policy & Privacy
JavaScript Examples
Policy
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 17/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 18/19
13/02/2024, 07:28 Sample Practice Problems on Complexity Analysis of Algorithms - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/?ref=lbp 19/19