Self Studies

Algorithms Test 1

Result Self Studies

Algorithms Test 1
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies

SHARING IS CARING

If our Website helped you a little, then kindly spread our voice using Social Networks. Spread our word to your readers, friends, teachers, students & all those close ones who deserve to know what you know now.

Self Studies Self Studies
Weekly Quiz Competition
  • Question 1
    1 / -0

    Consider the following functions from positives integers to real numbers

    1000, log2 n, √n, 1000/√n,

    The correct arrangement of the above functions in descending order of asymptotic complexity is:

    Solution

    Take very large number in order to compare the asymptotic complexity of the given

    If n = 210000

    √n = √210000 >> 1000

    log2 n = 10000

    1000 = 1000

    1000/√n = 1000/√210000 ≈ 0

    Descending order: √n, log2 n, 1000, 1000/√n

  • Question 2
    1 / -0

    What is the complexity of the following code?

    int count = 0;

    int i, j, k;

    for (i = m; i >= 0  ; i /= 3)

    {

    for(j = 1; j <= n ; j++)

    {

    while(k--)

    {

    count++;

    }

    }

    }       
    Solution

    Time complexity

    int count = 0;

    int i, j, k;

    for (i = 1; i < =  m; i *= 3)  // O(log3 m)

    {

    for(j = 1; j <= n ; j++)         // O(n)

    {

    while(k--)       // O(k)

    {

    count++;

    }

    }

    T(n) =  O(log3 m) × O(n) × O(k) = O(n.k.logm)   

  • Question 3
    1 / -0

    What it the time complexity of the below given block of code?

    int m,n,X,Y;
    for(m = 0; m<X; ++m)
    printf("EdTech: ");
    for(n = 0; n<Y; n++)
    printf("Testbook.com");

    Solution

    For First Loop: runs for X times

    T(X) = O(X);

    For Second Loop: runs for Y times

    T(Y) = O(Y);

    Total time complexity: T(X) + T(Y)

    ∴ time complexity = O(X + Y)

  • Question 4
    1 / -0

    What is the space complexity of the following binary search algorithm?

    int BinSearch(int a[], int n, int data)

    {

    int low = 0;

    int high = n-1;

    while (low <= high)

    {

    mid = low + (high-low)/2;

    if(a[mid] == data)

    return mid;

    else if(a[mid] < data)

    low = mid + 1;

    else

    high = mid - 1;

    }

    return -1;

    }

    Solution

    Binary search is also called a half-interval search. It compares the required value with a middle element.

    Generally, space complexity is the extra space that we are needed in the algorithm.

    In a binary search, we have just a comparison between the array element and data element to be searched.

    Since no extra space is needed: Space complexity: O(1)

    Important Points:

    In space complexity, an array that contains the original data is not included because it is already given and we have not added any extra space apart from that.

    The time complexity of binary search: O(log n) 

  • Question 5
    1 / -0

    Consider a given block of code in C?

    int main()
    {
    int p;
    printf("Enter the number: "); 
    scanf("%d",&p);
    bool x = Isprime(p);
    if(x)
    printf("p is prime");
    else
    printf("p is not prime");
    return 0;
    }

    What is the worst-case complexity for the optimal solution of function Isprime() to prove that a number (p) is a prime number or not?

    Note: 

    Input: p > 1

    Solution
    A prime is only divisible by 1 and itself.
    It is enough to calculate √p and check a number is divisible or not by other numbers
    The complexity of Ispime is O(√p).
    Code:
    #include<stdio.h>
    #include <math.h>
    bool Isprime(int);
    int main()
    {
    int p;
    printf("Enter the number: "); 
    scanf("%d",&p);
    bool x = Isprime(p);
    if(x)
    printf("p is prime");
    else
    printf("p is not prime");
    return 0;
    }
    bool Isprime(int p){
    int i;
    for(i = 2; i <= sqrt(p); i++)    \\ O(√p)
    {
    if(p%i == 0)
    return false;
    }
    return true;
    }
  • Question 6
    1 / -0

    Considering the equality \(\mathop \sum \limits_{j = 0}^n {j^2} = A\) and the following choices of A:

    I. Ω(n2)

    II. θ(n3)

    III. O(n4)

    IV. θ (n4

    The equality above remains correct if A is replaced by

    Solution

    \(A = \mathop \sum \limits_{j = 0}^n {j^2}\)

    A = 12 + 22 + 32 + 42 + 52 + 62 … n2

    \(A = \frac{{n\left( {n\; + \;1} \right)\left( {2n + 1} \right)}}{6}\)

    For given expression what holds true

    Θ(n3) → average case (true)

    O(n4) → worst case (true)

    Ω(n2) → best case (true)

    θ (n4) → average case (false)

    Option c is true.

  • Question 7
    1 / -0

    Give asymptotic upper and lower bound for T(n) given below. Assume T(n) is constant for \(n \le 2.\;T\left( n \right) = 4T\left( {\sqrt n } \right) + lo{g^2}n\)

    Solution

    \(T\left( n \right) = 4T\left( {\sqrt n } \right) + lo{g^2}n\)

    Consider n = 2m

    m = log n

    T(2m) = 4T(2m/2) + m2

    Put S(m) = T (2m/2)

    S(m) = 4 S(m/2) + m2

    Now use master’s theorem :

    a = 4 and b = 2, \({m^{log_b^a}} = \;{m^{log_2^4}} = \;{m^2}\)

    S(m) = m2 + m2

    So, Time complexity will be O(m2log m)

    Put the value of m

    Time complexity will be :  O (log2n log (logn))

Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Selfstudy
Selfstudy
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now