You are on page 1of 15

Algorithms

(Solutions for Vol‐1_Classroom Practice Questions)

1. Algorithm Analysis & 05. Ans: (c)


Asymptotic Notations Sol: When k=6
n
10n. log10  60000000

01. Ans: (a) 0.0001n2 = 1000000


Sol: f3 < f2 < f4 < f1
06. Ans: (a)
Sol: g1(n)  C. g2(n) for n  101
02. Ans: (b)
when n > 100, g1(n) = n3 up to n  10,000
Sol: log n  c. log log n for all n2 from the Big
and g1(n) = n2 , n  10,000
Oh definition log n O(log log n)
when n > 100, g2(n) = n3
n log n g2(n) is always greater than or equal to g1(n).
(a) 100 nlogn  c. for n  1
100
nx 07. Ans: (d)
(c) Lt y  0 Because 0 < x < y
n  n
Sol: 3n n
 c.n! for n  5 from Big oh notation
1
(d) 2n > c. nk for n  1 if k  3n n
 O(n!)
c
n
→ n!  3n for n  5

03. Ans: (b) h(n) is not O(f(n))


Sol: n(logn)10  C. n2 logn for n  252 → n! c.2 n log 2 n
for n  4

→ 2 n log 2 n
 c.3n n
for n  4
04. Ans:(a)
Sol: I  n  km  C.n m for n  n 0 and n  km  C.n m 08. Ans: (d)
for n ≥ n0 Sol: (a) 2n  n!  n  1 but
n! ≰ C. nlogn  n  no (false)
 n  k    n m
m
 
II  2 n 1  3.2 n for n ≥ 1 (b) 2n ≱ 2n!  n 1 (false)
(c) n! ≰ C. 2n  n  5 (false)
2 n 1
 O2 n

(d) nlogn  C. 2n  n  no and
III  2 ≰ C.2 for n ≥ 1
2n n
n!  C. 2n for n  1 (true)
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 2 : Algorithms

09. Ans: (b) 13. Ans: (b)


Sol: The upper bound cannot be more than n
and lower bound will be Ω (1) when the 14. Ans: (c)
loop terminates with the condition n % i = 0 Sol: T(n) = 1, n  2
for the first time.  
Tn   T n  K, n  2
Big O notation describes the tight upper K
n2 2
bound and Big Omega notation describes the
tight lower bound for a algorithm. The for 2  K  log n 2

loop in the question is run maximum n 2 K  log 2 n  K  log log 2 n


times and minimum 1 time. Therefore,
 
Tn   O n and Tn   1. 15. Ans: (c)
Sol: In all cases, when A[i ] =1 for all i = 1,n
10. Ans: (c) A[i] = 0; for all i = 1,n
Sol: Average case running time always less than
A [i] =1; for i =1,n/2
(or) equal to worst case Time.
n
 A(n) = O(w(n)) = 0; for i   1,...n
2
By definition of asymptotic notations.
The order of magnitude is (n).
Average lies always between the best and
the worst case inclusive. 16. Ans: (d)
Sol: Tn   2Tn  1  1
11. Ans: (c)
n n n n Because recursive calls (n –1) two times.
Sol: j      ..  1
1 2 4 8 Now,
1 1 1 1 1
 n     ...    n01 Tn  1  2Tn  2   1
1 2 4 8 n
 Tn   22Tn  2   1  1
 (n) [∵ Polynomial of degree 1]
 2 2 Tn  2   2  1

12. Ans: (d) Continue this we get


Sol: j is multiplied by 2 for every iteration  2 n 1  2 n  2  ...  2  1  2 n  1
it runs k times + 1 for condition fail.  2 n  1  1.2 n for n  1 = O 2 n  
k
n = 2  log n = k
It uses log 2 n   1 comparisons

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 3 : CSIT‐Postal Coaching Solutions

17. Ans: (a) (or)
Sol: T(n) = 2T(n – 1) + n
T(n) – T(n–1) = n, T(1) = 1
Let an = T(n)
T(n) – T(1) = 2+3+….n
an = 2an–1 + n ………(1)
n n  1
an – 2an–1 = n T(n) = 1+2+3+….n 
2
replace ‘n’ by n + 1
an+1 – 2an = n 20. Ans: (b)
E(an) – 2an = n Sol:  f 1n  requiresTn   2Tn  1  3Tn  2  1

 2 n 
(E – 2) an = n
Characteristic equation (E) = 0
 f 2n  requires n because i runs n times
E–2=0
  (n)
 characteristic root = 2
 Complementary function = C1 2n
21. Ans: (c)
Let an = An + B
Sol: f 1 8  2 * f 1 7   3 * f 6   1640
Substitute ‘an’ in equation (1)
f 1 7   2 * f 1 6   3 * f1 5  547
An + B = 2{A(n–1) + B} + n
 An = 2An + n B = –2A + 2B f 1 6   2 * f 1 5  3 * f 1 4   182

 –An = n By substituting ‘A’ f 1 5  2 * f 1 4   3 * f 1 3  61

A=–1 B=2 f 1 4   2 * f 1 3  3 * f 1 2   20


 The solution is T(n) = C1.2n – n – 2 f 1 3  2 * f 1 2   3 * f 1 1  7
By applying initial condition we get C1 = 2 f 1 2   2 * f 1 1  3 * f 1 0   2
n+1
 T(n) = 2 –n–2 f2(8) = 1640

18. Ans: (d)


f1(n) and f2(n) are same
Sol: log 2 log 2 n 

19.
Sol: T(n) = O(n2) Recursive Iteration

T(n) = n+(n–1) + (n–2) + ………..+ T (1)


n n  1

2
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 4 : Algorithms

06. Ans: (c)


2. Divide & Conquer
Sol: Case (i): If the array elements are in the
increasing order then the quick sort takes
01. Ans: (a)
number of comparisons are
Sol: Divide and conquer strategy.
n n  1
t1 
2
02. Ans: (c)
Case (ii): If the array elements are randomly
Sol: To make two sorted arrays as single sorted
distributed then the quick sort takes number
array it requires m + n comparisons.
of comparison are

03. Ans: (b) 


t 2  2n  1 log en  0.577  4n 
 t1 > t 2
06. Ans: (b)
Sol: 07. Ans: (b)
[20] [47] [15] [8] [9] [4] [40] [30] [12] [17]
Sol: By applying divide and conquer concept one
Pass 1: [20 47] [8 15] [4 9] [30 40] [12 17]
list would have 1/5 elements and the other
Pass 2: [8 15 20 47] [4 9 30 40] [12 17] would have 4/5 of the no. of elements ‘n’
comparisons are required for fixing up the
04. Ans: (b)
Sol: In merge-sort algorithm number of splits are pivot.
proportional to height and in each level
08. Ans: (c)
work done is n2
Sol: If we choose pivot randomly, Randomized
 Total Time Complexity O(n2 log n)
quick sort still may have worst case time of
The Complexity of Merge Sort for n
O(n2).
elements is O(n log n).
It can be O(n log n) if pivot always divides
05. Ans: (a) the array into two equal sub parts.
Sol: In the worst case the selected pivot element
will be placed in either first (or) last position 09. Ans: (c)
Then the required recurrence equation is Sol: Binary search takes time of O(log n) for a
T(n) = T(n – 1) +  (n) set of n-elements. Total time for n-elements
By solving using substitution method we get = O(n log n)
T(n) = O(n2)
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 5 : CSIT‐Postal Coaching Solutions

10. Ans: (d)
(or)
Sol: If we subtract each number by 1 then we get
T(2k) = 3T(2k1)+1
3
the range [0,n -1]. Considering all number
ak = 3ak1+1
as 3-digit base n: each digit ranges from 0 to
(E3)ak = 1
n3 – 1. Sort this using radix sort. This uses
ak = C13k
only three calls to counting sort. Finally, add
1 1
1 to all the numbers. Since there are 3 calls, ak = c3k a k  
1 3 2
the complexity is O(3n)  O(n).
1 1 3
a k  c  3k  ao  c   1 c 
2 2 2
11. Ans: (b)
Sol: Using Divide and Conquer method not more 3 k 1  3k 1  1 
ak   3    
2 2  2 
 3n 
than   2  comparisons are required in
 2 
all cases of input. 15.
Sol: T(n) = T(n/2) + n, T(1) = 1
12. Ans: 148 Master Theorem
Sol: Minimum number of comparisons required a = 1, b = 2, k = 1, p = 0
to find the minimum and maximum of 100 Since a<bK, so it is case (3) of master
numbers = 1.5 (100) – 2 = 148 theorem.
 T(n) = O(n)
13. Ans: (a) Case 3: 1 < 21
Sol: After comparing the key with the middle
element, the search is made either in the left 16. Ans: (a)
or right sublist with n/2 elements. Sol: T(n) = 2T (n/2) + log n
 T(n) = T(n/2)+k, where ‘k’ is constant. By using Master Theorem, a = 2, b = 2,
k = 0, p =1
14. Ans: (b) As a > bk, so it is case(i) of Master Theorem
Sol: T (2k) = 3 T (2k-1) + 1
a
T (n) = ( n log b )
Let n = 2k = (n)
T(n) = 3 T(n/2) +1
Solve it using back substitution.
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 6 : Algorithms

17. Ans: (a) T(2k) = 2T(2k-1)+n


Sol: Applying Master Theorem; case ‘3’ holds ak – 2k-1 = 2k
and hence T(n) is O(n); (-2)ak = 22k
a = 3, b = 4, K = 1 ak = c2k
Since a<bK so it is Case 3 of master 2.2 k
ak  = 2c(k,1) 2k-1
theorem.  2
 T(n) = O(n) = 2k2k-1
Master Theorem: a1 = c1
a = 3, b = 4, k = 0, p = 0 ak = 2k+k2k
3 > 4. On log 34  T(2k) = 9k = n+nlog2n  O(n log n)

18. Ans: (b) 20. Ans: (a)


Sol: Applying master-theorem Case-III holds.
n
Sol: Tn   2T   n , n  2
2 T(n) = T(n/3)+n/2

T(1) = 1  T(n) = (n) (Master Theorem Case 3.a)


Master Theorem:
a = 2, b = 2, k = 1/2, p = 0 3. Greedy Method
1/2
2 > 2 O(nlog22) = O(n)
01.
19. Ans: (c) Sol: Apply Kruskal’s Algorithm

Sol: It is  (n log n) is also O (n2) & A 2 B


3
O(n log n) is not Ω (n2).
3 C
If we use binary search then there will be 4

log 2 n! comparisons in the worst case, E D


Cost=12
which is (n log n). But the algorithm as a
whole will still have a running time of (n2) Another possibility

on average because of the series of swaps A 2 B


3
required for each insertion.
3 4 C
(or)
T(n) = 2T(n/2)+n, T(0) = T(1) = 1 E D
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 7 : CSIT‐Postal Coaching Solutions

02. Ans: (d) 06. Ans: (b)
Sol: There can be multiple minimum cost Sol: cost of MST with ‘4’ vertices is 3+4+6
spanning trees under the presence of non- Cost of MST with ‘5’ vertices is 3+4+6+8
distinct edge costs. In general cost for ‘n’ vertices we have
= 3+4+6+8+…………+2n2
03. Ans: (b) = n2n+1
Sol: 2 j
b g
07. Ans: (c)
5 5 6
Sol:
3 8 8 10
10 + 6 + 3 + 4 + 8 = 31
c 2 4 3 4
i
1 d h
4 6
a 4
2 1 2
3
f 08. Ans: 8
e Sol: In the given graph V = 9, E = 13
Weight =1+2+3+2+8+4+5+4+2=31 MST, must contains V – 1 number of edges
04. Ans: (b) = 8 edges.

Sol: When vi is connected to vi+1, the edge cost is 1


2. there will be (n-1) such edges with a cost 1
1
of ‘2’. 1
1
Therefore total cost = 2(n–1) 2

= 2n-2 09. Ans: (d)


Sol: Kruskal’s Algorithm uses min Heap to keep
(or)
n n the list of edges (m log m)
 2 | i  i  1 | 2 | 1 | 2 | n  1 | 2n  2
i 1 i 1
With the Union-Find data structure
implemented this way, Kruskal’s algorithm
can be analyzed. The sorting of the edges
05. Ans: (d)
can be done in O(mlogn) which is O(mlogn)
Sol: If there are multiple edges in the graph with
for any graph (why?). For each edge (u,v)
the minimum weight ‘w’, then it is not
we check whether u and v are in the same
necessary that the specific edge with cost tree, this is done with two calls to Find
‘w’ must be present in all spanning trees. which is O(logn), and we union the two if
Rest all options are correct. necessary which is O(1). Therefore the loop
There may be many edges of weight w in is O(mlogn). Hence the total time
the graph and e. complexity is O(mlogn).
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 8 : Algorithms

10. Ans: (d) 14. Ans: (d)


Sol: (a) (a1b), (d1f), (b2f), (d2c), (d3e) valid Sol: In Dijkstra’s Shortest Path Algorithm, we
(b) (a1b), (d1f), (d2f), (b2f), (d3e) valid always consider the vertex which are
(c) (d1f), (a1b), (d2c)., (b2f),(d3e) valid reachable from the source with last cost, and
(d) (d1f),(a1b),(bf2),(de3), (d2c) invalid update cost label of the vertex, if the present
cost is minimum than the previous cost.
11. Ans: (a) Apply Greedy based Dijkstra’s Algorithm.
Sol: Number of edges in the shortest path can be
A B C D E F G T
determined as a result of Dijkstra’s single
S 4 ③  7    
source shortest path algorithm.
B ④  7    

12. Ans: (b) A ⑤ 7    


Sol: 2 7 C 7 ⑥   
P Q R S T U -Invalid (not in
(a)
1 4 3 increasing order) E 7  8  SACET

2 4
 The shortest path from S → T is SACET.
(b) P Q R U S T -Valid
1 3 4
15.
2 7 Sol:
(c) P Q R U T S -Invalid 0.13
1 3 4
0.05 0.08 0.11 0.17 0.19 0.40
7 3 b d f c a e
(d) P Q T R U S -Invalid
1 2 4
0.24

13. Ans: (c) 0.11 0.13 0.17 0.19 0.40


Sol: Edges and vertices of the graph can be f c a c
maintained in the form of a heap data 0.5 0.08

structure to have a linear time complexity b d

algorithm.

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 9 : CSIT‐Postal Coaching Solutions

16. Ans: (a)
0.3 0.24 0.40
e Sol: Obtain the Huffman Encode tree by
0.13
0.17 0.19 0.11 applying optimal merge pattern algorithm.
c a f
Assign ‘0’ to the left branch and ‘1’ to the
0.05 0.08
b d right branch in the encode tree. Collect the
stream of binary bits to get the codes of the
0.24 0.36 0.40 message.
e
1
0.11 0.13 0
0.17 0.19 1
f c a 2
0
0.05 0.08 1
1
b d 0 4
1
1
8 1
0.6 0.40 0
1 1
e 16
0 1
0.24 0.36
1 1 1 1 1 1
32 32 16 8 4 2
0.13 0.17 0.19 f e d c b a
0.11
f 17. Ans: (d)
0.05 0.08 Sol: The average Number of bits /message is
obtained by using the formula
1.0 n
0 1 d i 1
i * qi
0.40 .
0.6 di= distance from root to message i
0 1
e qi = probability of message i
0.24 0.36
0 0 1
1 1 1 1 1 1
1;  2 ;  3;  4 ;  5
0.11 0.13 0.17 0.19 2 4 8 16 32
f 0
1
c a 1 1 3 4 5 5
= 1   2    
0.05 0.08 2 4 8 16 32 32
b d 16  16  12  8  10

32
Prefix codes:
32  30
=
a  111 ; b  1010 ; 32
62
c  110 ; d  1011 ; e  0   1.9375
32
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 10 : Algorithms

09. Ans: (c)


4. Graph Technique, Components, Heaps
Sol: In selection sort, each iteration takes one
swap in the worst case. Hence it required
01. Ans: (b)
O(n) swaps in the worst case.
02. Ans: (b)
Sol: Binary search – O(log n) 10. Ans: (b)
Insertion sort – O(n) Sol: Algorithm to determine leaders in an array:
Merge sort  O(n log n) int max = a[n]
Selection sort – O(n2) for i  n – 1 to 1 by –1
{
03. Ans: (d)
Sol: Nodes 2, 3, 5 are the Articulation points. if (a[i] > max)
{
04. Ans: (b)
Sol: In the array representation of Binary Tree, print a[i];
max = a[i];
the parent is at location i/2, where as left
}
child is at 2i and right child at 2i + 1.
}
05. Ans: (d) While scanning the array from right to left
Sol: a, b, f, e, h, g is not possible as one cannot remember the greatest element seen so far
visit ‘e’ after ‘f’. and compare it with the current element to
test for leadership.
06. Ans: (c)
Sol: In order to find 7th smallest element, we
11. Ans: (a)
have to perform ‘7’ deletion operations so it
Sol: Merge sort takes a time of O(n log n) in all
takes O(7logn) = Θ(logn).
cases of input. Whereas other sorting
07. Ans: (c) techniques have complexity of O(n2) in
worst case.
08. Ans: (a)
Sol: Smallest element lie at the leaf level. Which
12. Ans: (c)
has roughly n/2 elements. Which would
Sol: This is the case when the graph is
require number of comparisons and number
represented by cost Adjacency matrix.
of elements.
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 11 : CSIT‐Postal Coaching Solutions

13. Ans: (c) The height of a Max Heap is (logn). While
Sol: The remaining choices violates the FIFO insertion, we need to traverse from leaf
discipline of the queue and hence are not element to root (in worst). If we perform
valid BFS traversals. binary search for finding the correct position
then we need to do (loglogn) comparisons.
14. Ans: (a)
In reality, it is not possible to perform binary
Sol: To sort ‘n’ elements using selection sort it
search on elements from leaf to root as they
requires O(n) swaps.
are not in sequence.
15. Ans: (c)
Sol: In option (a) (13) cannot be the child of (12) 17. Ans: (b)
in max-Heap. Sol: Trees in option (d) violate the property of
In option (b) (16) cannot be the child of Max-Heap. Tree (a) satisfies the property of
(14). Max-Heap, but it is not a complete Binary
In option (d) (16) cannot be the child of Tree. In (c) and (d), heap property is not
smaller value node. satisfied between 5 and 8.
In A, s[3] which is the left child of a[1] is  Tree (b) is the correct answer.
greater than the parent (13>12). In B, also
a[3]>a[1] (16>14). In D, a[6] which is right 18. Ans: a-r, b-p, c-s, d-q
child of a[2] is greater than a[2] (16>12).
19. Ans: a-q, b-r, c-s, d-p
16. Ans: (d)
Sol: 25
20. Ans: (d)
14 16
Sol: If we subtract each number by 1 then we get
13 10 8 12 the range [0,n3 -1]. Considering all number
as 3-digit base n: each digit ranges from 0 to
After deleting (25) n3 – 1. Sort this using radix sort. This uses
16 only three calls to counting sort. Finally, add
14
1 to all the numbers. Since there are 3 calls,
14 12 After deleting (16)
13 12 the complexity is O(3n)  O(n).
13 10 8
10 8
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 12 : Algorithms

21. Ans: (c) A A


Sol: Tree Representation of the array is r
B C
B C
89
u
D E F G D E F G
40 v
19
G
H
17 12 10 2 (G) v (B.T.T)

5 7 11 6 9 70
25. Ans: (d)
Sol: Based on Kirchoffs Theorem
22. Ans: (d)
Sol: If x is found at loc ‘1’
26. Ans: (a)
 1 comparison
Sol:
x is found at loc ‘2’ 32
 2 comparisons
30 25
x is found at loc ‘3’
15 12 20 16
 3 comparisons
x is found at loc ‘n’
27. Ans: (d)
 ‘n’ comparisons
Sol: Since the degree of u & v in ‘G’ is at least 2.
Total comparison = n(n+1)/2
n(n  1)
28. Ans: (d)
Therefore average = 2
n Sol: 1

3 5 6
23. Ans: (c)
Sol: (c) is always true but (b) & (d) are true in 8 9

only few cases. (a) is never true. Tree for option (a) ‘1’ in at top but not

24. Ans: (c) 9


Sol: If ‘u’ is visited before ‘v’, then the distance 6 3 1
from ‘r’ to ‘u’ will be less than or equal to
8 5
the distance from ‘r’ to ‘v’ as demonstrated Tree for option (b) ‘8’ is greater than ‘6’ not possible
in the example given below
ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

: 13 : CSIT‐Postal Coaching Solutions

9 30. Ans: (b)
Sol: Consider the sample tree given below.
3 6 8
Binary search to be applied along the path
5 1 which has log n elements.
Tree for option (c) ‘5’ is greater than ‘3’
and hence not Possible
1000
9
log n
80 90
5 6 8

65 10 20 35
3 1
Tree for option (d) is max Heap x

29. Ans: (a)


Sol: The height of a Max Heap is (logn). While
9
insertion, we need to traverse from leaf
5 6 8 element to root (in worst). If we perform

3 1
binary search for finding the correct position
then we need to do (loglogn) comparisons.
(a) Original tree
In reality, it is not possible to perform binary
Inserting 7
9 search on elements from leaf to root as they
7 6 8 are not in sequence.

3 1 5
31. Ans: (b)
Inserting 2
9 Sol: Procedure Heapify with adjust would
require time of O (n) for n-elements, now
7 6 8
with addition n-elements, total being ‘2n’
3 1 5 2 would still be order of O (n).

In the array it is :10,7,9,8,3,1,5,2,6,4


We can reduce the problem to building Heap
10 for 2n elements. Time complexity for
Inserting ‘10’
7 8 building heap is O(n).
9

3 1 5 2 6 4

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata



: 14 : Algorithms

06. Ans: 34
5. Dynamic Programming
Sol:
i A
0 1 2 3 4 5
i q p q r r
01. Ans: (a)
0 0 0 0 0 0
p 0 0 1 1 1 1 p
q 0 1 1 2 2 2 q
02.
p 0 1 2 2 2 2
Sol: for i  1 to n r 0 1 2 2 3 3
q 0 1 2 3 3 3 r
for j  1 to n
r 0 1 2 3 4 4 r
if (A[i ,j ] = 0) then P [i, j] = 0; p 0 1 2 3 4 4
else P[i ,j ] =1;
for i  1 to n LCS = pqrr

for j  1 to n and length of longest common subsequence


=x=4
for k  1 to n
Similarly remaining LCS are qsqr, qprr
P[i ,j ] = min{P[i,k]+P[k,j], P[i,j]}
Time complexity is O (n3). y=3
 x + 10y = 4 + 10(3) = 34.

03. Ans: (c)


Sol: Using LCS algorithm, in Dynamic 07. Ans: (b)

programming we can write Sol: Apply Principle of optimality

expr1 = 1+l(i1,j1); and


08. Ans: (c)
expr2 = max (l(i1, j), l(i,j1))

09. Ans: (c)


04. Ans: (b)
Sol: It is based on Dynamic programming.
Sol: We can compute using either CMO (or)
Use the recurrence that arises in this
RMO of L(M,N).
problem and multiply as

05. Ans: (a)


(M1 (M2  M3))  M4 = 19,000

Sol: As per the given recurrence, it is based on See the recurrence of Matrix Chain Product.

the principle of optimality.

ACE Engineering Publications Hyderabad|Delhi|Bhopal|Pune|Bhubaneswar| Lucknow|Patna|Bengaluru|Chennai|Vijayawada|Vizag |Tirupati | Kukatpally| Kolkata

You might also like