Sunday, September 22, 2024
Google search engine
HomeData Modelling & AISample Practice Problems on Complexity Analysis of Algorithms

Sample Practice Problems on Complexity Analysis of Algorithms

Prerequisite: Asymptotic Analysis, Worst, Average and Best Cases, Asymptotic Notations, Analysis of loops.

Problem 1: Find the complexity of the below recurrence:  

              { 3T(n-1), if  n>0,
T(n) =   { 1, otherwise

Solution:  

Let us solve using substitution.

T(n) = 3T(n-1)
       = 3(3T(n-2)) 
       = 32T(n-2)
       = 33T(n-3)
       … 
       …
       = 3nT(n-n)
       = 3nT(0) 
       = 3n

This clearly shows that the complexity of this function is O(3n).

Problem 2: Find the complexity of the recurrence:  

             { 2T(n-1) – 1, if n>0,
T(n) =   { 1, otherwise

Solution: 

Let us try solving this function with substitution.

T(n) = 2T(n-1) – 1
       = 2(2T(n-2)-1)-1 
       = 22(T(n-2)) – 2 – 1
       = 22(2T(n-3)-1) – 2 – 1 
       = 23T(n-3) – 22 – 21 – 20
          …..
       …..
       = 2nT(n-n) – 2n-1 – 2n-2 – 2n-3
          ….. 22 – 21 – 20

       = 2n – 2n-1 – 2n-2 – 2n-3
          ….. 22 – 21 – 20
          = 2n – (2n-1) 

[Note: 2n-1 + 2n-2 + …… +  20 = 2n – 1]

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.

Problem 3: Find the complexity of the below program: 

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




/*package whatever //do not write package name here */
 
static void function(int n)
{
    if (n==1)
       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.


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.


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()
     
# The code is contributed by Nidhi goel.


C#




/*package whatever //do not write package name here */
 
public static void function(int n)
{
    if (n==1)
       return;
    for (int i=1; i<=n; i++)
    {
        for (int j=1; j<=n; j++)
        {
            Console.Write("*");
            break;
        }
          Console.WriteLine();
    }
}
// The code is contributed by Nidhi goel.


Solution: Consider the comments in the following function. 

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;
        }
    }
}


Time Complexity: O(n), Even though the inner loop is bounded by n, but due to the break statement, it is executing only once.

Problem 4: Find the complexity of the below program: 

CPP




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++;
}


Java




static 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++;
}
 
// This code is contributed by rutvik_56.


C#




static 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++;
}
 
// This code is contributed by pratham76.


Javascript




<script>
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++;
}
 
// This code is contributed by umadevi9616
</script>


Solution: Consider the comments in the following function. 

CPP




void function(int n)
{
    int count = 0;
    for (int i=n/2; i<=n; i++)
 
        // Executes O(Log n) times
        for (int j=1; j<=n; j = 2 * j)
 
            // Executes O(Log n) times
            for (int k=1; k<=n; k = k * 2)
                count++;
}


Time Complexity: O(n log2n).

Problem 5: Find the complexity of the below program: 

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




static 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++;
}
 
// This code is contributed by Pushpesh Raj.


Solution: Consider the comments in the following function. 

CPP




void function(int n)
{
    int count = 0;
 
    // outer loop executes n/2 times
    for (int i=n/2; i<=n; i++)
 
        // middle loop executes  n/2 times
        for (int j=1; j+n/2<=n; j = j++)
 
            // inner loop executes logn times
            for (int k=1; k<=n; k = k * 2)
                count++;
}
 
// The code is contributed by Nidhi goel.


Java




static void function(int n)
{
    int count = 0;
 
    // outer loop executes n/2 times
    for (int i=n/2; i<=n; i++)
 
        // middle loop executes n/2 times
        for (int j=1; j+n/2<=n; j = j++)
 
            // inner loop executes logn times
            for (int k=1; k<=n; k = k * 2)
                count++;
}
 
// This code is contributed by Aman Kumar


Javascript




function function(n)
{
    let count = 0;
 
    // outer loop executes n/2 times
    for (let i= Math.floor(n/2); i<=n; i++)
 
        // middle loop executes  n/2 times
        for (let j=1; j+n/2<=n; j = j++)
 
            // inner loop executes logn times
            for (let k=1; k<=n; k = k * 2)
                count++;
}
 
// The code is contributed by Nidhi goel.


Python3




def function(n):
    count = 0
 
    # outer loop executes n/2 times
    for i in range(n//2, n+1):
 
        # middle loop executes  n/2 times
        for j in range((1, n//2 + 1):
 
            # inner loop executes logn times
            for k in range(1, n+1, 2):
                count++
 
# The code is contributed by Nidhi goel.


C#




using System;
 
public static void function(int n)
{
    int count = 0;
 
    // outer loop executes n/2 times
    for (int i=n/2; i<=n; i++)
 
        // middle loop executes  n/2 times
        for (int j=1; j+n/2<=n; j = j++)
 
            // inner loop executes logn times
            for (int k=1; k<=n; k = k * 2)
                count++;
}
 
// The code is contributed by Nidhi goel.


Time Complexity: O(n2logn).

Problem 6: Find the complexity of the below program: 

CPP




void function(int n)
{
    int i = 1, s =1;
    while (s <= n)
    {
        i++;
        s += i;
        printf("*");
    }
}


Solution: We can define 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 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).

Problem 7: Find a tight upper bound on the complexity of the below program: 

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("*");
            }
}


Solution: Consider the comments in the following function. 
 

CPP




void function(int n)
{
    int count = 0;
 
    // executes n times
    for (int i=0; i<n; i++)
 
        // executes O(n*n) times.
        for (int j=i; j< i*i; j++)
            if (j%i == 0)
            {
                // executes j times = O(n*n) times
                for (int k=0; k<j; k++)
                    printf("*");
            }
}


Time Complexity: O(n5)

This article is contributed by Mr. Somesh Awasthi. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

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 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments