Professional Documents
Culture Documents
Algorithm Group 4
Algorithm Group 4
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
1 + 2 +... + log2(n)
sum =
So T(n)=
35)
a)
2+5+3+7+8=25
second loop
k=2
k=4
k=8
k=16
k=32
b)
both loop executes n times ,and they are not nested
t(n)=
index I,j,k;
j=0;
k=1;
for(i=1;i<=n;i++)
j=j+a[i];
k=k+k;
Return j + k
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)
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)
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
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),
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.
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.
(1.1) ≠ (1.2),
So, the answer is False