Professional Documents
Culture Documents
!
Suppose that each row of an n*n array A consists of 1’s and 0’s such that, in any row of A, all the 1’s come before any 0’s
in that row. Assuming A is already in memory, describe a method running in O(n) time for finding the row of A that
contains the most 1’s.
(http://stackoverflow.com/questions/43170478/can-the-elements-of-a-two-dimensional-array-be-processed-for-some-
output-in-on)
ANS 1.A
1. Start from first row and scan from right to left to encounter the first 1
2. When you reach the 1, update your answer. Suppose, the column is j.
3. Now for second row, check if jth column holds 1.
o If it holds 1, that means there can be 1 in right too. So try to move right until finding the
rightmost 1 and update the answer and j
o If 0, we won't need to go left or right as we have better result already for row 1
4. Repeat these steps for all rows and maximize the answer.
This algorithm will scan at most n elements in total for all rows. So time complexity is O(n).
Moreover, you can use binary search to find the rightmost 1 for each row which will require less scan.
This algorithm takes ‘n’ operations horizontally and ‘n’ operations vertically, total ‘2n’ operations at the worst.
QUES 1.B
ANS 1.B (DSA SS ZG519 EC-2R SECOND SEM 2016-17 Answer Sheet)
Put these function in the ascending order of their growth in terms of n: 2^((log n)^0.5), 2^n, 2^(n/2), n^(4/3),
n(log n)^3, n^(log n), 2^(n^2), n!
!
Partial correct answer: 2^((log n)^0.5), 2^(n/2), 2^n, n!, 2^(n^2)
(http://stackoverflow.com/questions/43124573/big-oh-ascending-order-of-some-functions-fn/)
=> The way you solve this problem is by seeing which function approaches infinity fastest as n approaches infinity.
Suppose you have some functions:
f(n) = n!
g(n) = n
h(n) = 3^n
You can compare any two functions by evaluating their ratio as n approaches infinity. For example:
Lim f(n) /
n->∞ / g(n)
If the result is greater than 1, then f(n) is asymptotically greater than g(n). If the result is 1, then they are
asymptotically the same. If the result is less than one, then f(n) is asymptotically less than g(n).
You may find that some are easy to solve, like f(n) and g(n), and you can simplify the expression to get a definite
value at the limit.
f(n) / = n! / = n (n-1)! / = (n-1)!
/ g(n) / n / n
The limit as n goes to infinity of this expression is infinity, which means that f(n) is asymptotically greater
than g(n).
Other expressions are not as simple. If evaluating the limit gives you an indeterminite form, you need to use
L'Hôpital's rule.
According to L'Hôpital's rule, we can evaluate a limit by substituting g'(n) and h'(n).
QUES 1.C
ANS 1.C
(DSA SS ZG519 EC-2R SECOND SEM 2016-17 Answer Sheet)
!
QUES 2.A
ANS 2.A
((T1) Algorithm Design and Applications (Michael Goodrich) (2015).pdf)
!
!
#
Yellow marked as min:
2, 5, 16, 4, 10, 23, 39, 18, 26, 5 --- 1st pass (No swaps)
2, 5, 16, 4, 10, 23, 39, 18, 26, 5 --- 2nd (Swap between 5 and 4)
2, 4, 16, 5, 10, 23, 39, 18, 26, 5 --- 3rd
2, 4, 5, 16, 10, 23, 39, 18, 26, 5 --- 4th
2, 4, 5, 5, 10, 23, 39, 18, 26, 16 --- 5th
2, 4, 5, 5, 10, 23, 39, 18, 26, 16 --- 6th
2, 4, 5, 5, 10, 16, 39, 18, 26, 23 --- 7th
2, 4, 5, 5, 10, 16, 18, 39, 26, 23 --- 8th
2, 4, 5, 5, 10, 16, 18, 23, 26, 39 --- 9th
QUES 2.B
ANS 2.B
!
!
Analysis:
#
QUES: 2.C
ANS 2.C
(DSA SS ZG519 EC-2R SEM-2 2016-17)
!
!
!
!
#
QUES 2.D
ANS 2.D
(http://www.geeksforgeeks.org/merge-k-sorted-arrays/ )
“Merge k sorted arrays - Given k sorted arrays of size n each, merge them and print the sorted output.”
A simple solution is to create an output array of size n*k and one by one copy all arrays to it. Finally, sort the
output array using any O(n Log n) sorting algorithm. This approach takes O(nk Log nk) time.
We can merge arrays in O(nk Log k) time using Min Heap. Following is detailed algorithm.
1. Create an output array of size n*k.
2. Create a min heap of size k and insert 1st element in all the arrays into a the heap.
3. Repeat following steps n*k times.
a) Get minimum element from heap (minimum is always at root) and store it in output array.
b) Replace heap root with next element from the array from which the element is extracted. If the array doesn’t have
any more elements, then replace root with infinite. After replacing the root, heapify the tree.
Time Complexity: The main step is 3rd step, the loop runs n*k times. In every iteration of loop, we call heapify which
takes O(Logk) time. Therefore, the time complexity is O(nk Logk).
QUES 3.A
AND 3.A
!
#
QUES 3.B
ANS 3.B
!
!
#
QUES 3.C
ANS 3.C
!
One drawback of quadratic probing:
!
!
QUES 3.D
ANS 3.D
!
!
!
#
QUES 4.A
ANS 4.A Binary search tree using 31, 41, 25, 59, 29, 27, 12, 14; draw tree after each insertion.
(BST Visualizer: https://www.cs.usfca.edu/~galles/visualization/BST.html)
!
!
!
#
QUES 5.A
!
Pseudo-code to count no of inversions in a sequence a1,a2,…….an (ai, aj) is inversion pair if ai >aj and I < J.
http://www.cs.cmu.edu/~ckingsf/class/02713-s13/lectures/lec12-inversions.pdf
(http://stackoverflow.com/questions/337664/counting-inversions-in-an-array)
Found it in O(n * log n) time by the following method:
Here is an example run of this algorithm. Original array A = (6, 9, 1, 14, 8, 12, 3, 2)
6 is in the 4th position of array B, thus there are 3 inversions. We know this because 6 was in the first position in array A,
thus any lower value element that subsequently appears in array A would have an index of j > i (since i in this case is 1).
2.b: Remove A[1] from array A and also from its corresponding position in array B (bold elements are removed).
A = (6, 9, 1, 14, 8, 12, 3, 2) = (9, 1, 14, 8, 12, 3, 2)
B = (1, 2, 3, 6, 8, 9, 12, 14) = (1, 2, 3, 8, 9, 12, 14)
Continuing in this vein will give us the total number of inversions for array A once the loop is complete.
Step 1 (merge sort) would take O(n * log n) to execute. Step 2 would execute n times and at each execution would
perform a binary search that takes O(log n) to run for a total of O(n * log n). Total running time would thus be O(n * log
n) + O(n * log n) = O(n * log n).
--***--
QUES 5.B
!
ANS 5.B
(https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-
graphs)
!
!
!
#
QUES 5.C
!
ANS 5.C
!
!
!
!
!
!
#