Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Answer:

34)

We have two loops here ,lets first consider outer while loop n becomes   with
each iteration ,until it is less than 1 ,so this execute in log2(n) time.

Now consider inner loop , to find the time complexity let us look at how many
times the inner loop will iterate for the values of i (i.e. for each iteration of the
outer loop):

i:n   = 1 iteration

i:n/2 = 2 iterations

i:n/4 = 3 iterations

... so on

so time will execute in

1 + 2 +... + log2(n)

sum =

So T(n)=

35)

a)

first loop will add all the elements

2+5+3+7+8=25

second loop

k=2

k=4
k=8

k=16

k=32

Final output will be =25+32=57

b)
both loop executes n times ,and they are not nested

so simply t(n) will be

t(n)=

c) you can do this by one loop itself

int add_them(int n,int A[ ]) {

index I,j,k;

j=0;

k=1;

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

j=j+a[i];

k=k+k;

Return j + k

but this will not improve t(n)


36)
a)

The best complexity of this algorithm is O(1) when the the first two elements are
equal.

b)

The worst case complexity of this algorithm is O(n4) when the complete 2d array is
traversed.
c)

int any_equal(int n, int A[]) {

for(i = 1; i <= n; ++i) {

for(j = i; i <= n && A[i][i] != A[i][j]; ++j);

if(arr[i][i] == arr[i][j])

return 1;

return 0;

d)

When no two elements of the array A are equal (meaning it does not contain any
duplicates), the algorithm returns 0.

e)

When the array A contains atleast one duplicate, the algorithm returns 1
37)
rem(int x,int n,int p){

if (n==1)
return x%p;
else
return rem(x,n/2,p);
}

NOTE: This program does lg n +1 function calls. Thus its complexity =   (lg n)

38)

39)

n^2sin(n) != O(n)
because
n^2sin(n) <= c*n is false as
nsin(n) <= c as the value of n increases, nsin(n) crosses the value of c so that
the given equation evaluates to false
so it is not O(n)

it is not   (n) because


if f(n) =   (n) then
n^2 sin(n) >= c*n
but we know sin(n) get value of 0 periodically
so LHS = 0 but RHS is always >0
so f(n) !=   (n)
40)

1. TRUE,
Here, to understand how the answer is true, we have to understand the Big-
Oh notations:-
If f(n) and g(n) are both the functions
O(g(n)) = { f(n) : There exist positive constant c and n0 such that 0 ≤ f(n) ≤ c
g(n), for all n ≤ n0}

So, we can say that the right hand side should be asymptotically greater than
its left hand side for the correctness.
Now, let's check one by one,
L.H.S = f(n) + g(n)
R.H.S = O(max(f(n), g(n)))

Here, on the right hand side, one of both f(n) and g(n) will be greater. So, it
will be absolutely correct.

For example,
f(n) = n
g(n) = n2

Then, L.H.S = f(n) + g(n) = n2 + n


R.H.S = O(max(f(n), g(n))) = O(max(n, n2)) = O(n2)
L.H.S = R.H.S

2. FALSE,
Here, we have to first understand the definition of Omega notation.
If f(n) and g(n) are both functions,
Ω(g(n)) = { f(n) : there exist positive constants c and n0 such that 0 ≤ cg(n) ≤
f(n) for all n ≥ n0 }

So, we can say that f(n) should be asymptotically greater than g(n),

Here, L.H.S = f2(n) = (f(n))2


R.H.S = f(n)

Now, by taking counter example as n = 1/n,


L.H.S = f(1/n2)
R.H.S = f(1/n)
1/n2 is not greater than 1/n.

Therefore, the answer is False.

3. FALSE
Here, we have to understand two definitions,
Little Oh notation :-
We can say that the function f(n) is o(g(n)) if for any real positive constant c,
there exists an integer constant n0 ≤ 1 such that f(n) > 0.

Big Theta Notation :-


Θ(g(n)) = { f(n) : there exist positive constants c1, c2 and n0 such that 0 ≤
c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }

So, f(n) =  Θ(g(n)) iff f(n) = Ω(g(n)) and f(n) = O(g(n)) or in other words
Big-Oh Notation and Big-Omega Notation should be equal.

So, Big-oh notation


f(n) + o(f(n)) = O(max(f(n), o(f(n))) = O(o(f(n))) ......(1.1)........From part (a)

And Big-Omega Notation,


f(n) + o(f(n)) = Ω(min(f(n), o(f(n))) = Ω(f(n)) ..........(1.2)....

(1.1) ≠ (1.2),
So, the answer is False

You might also like