Algorithms Solutions: 1 2 3 1 2 N I I+1 J I I+1 J
Algorithms Solutions: 1 2 3 1 2 N I I+1 J I I+1 J
Solutions
1. Which of the following is not an in-place algorithm?
Explanation:
An in-place algorithm is an algorithm which uses a constant amount of extra space apart from
input. Merge sort uses an extra O (n) space in the merging part.
3. Given two arrays of numbers a1, a2, a3,…an and b1, b2, .. bn where each number is 0 or 1, the
fastest algorithm to find the largest span(i, j) such that ai + ai+1, ….aj = bi, bi+1, .. bj. or report that
there is not such span,
4. What is the return value of following function for arr[ ] = {9, 12, 2, 11, 2, 2, 10, 9, 12, 10, 9,
11, 2} and n is size of this array?
1
return x;
}
(a) 0 (b) 9
(c) 12 (d) 2
Explanation:
Note that 9 is the only element with odd occurrences, all other elements have even occurrences.
If the input array has all elements with even occurrences except one, then the function returns the
only element with odd occurrences. Note that XORing an element with itself results 0 and XOR
of 0 with a number x is equal to x.
Explanation:
The expression multiplies an integer with 3.5. For example, if x is 4, the expression returns 15. If
x is 6, it returns 21. If x is 5, it returns 17.
Explanation:
The expression simply turns off the rightmost set bit. For example, if x = 14 (1110), it returns 12
(1100).
2
7. Consider the C function given below. Assume that the array listA contains n (> 0) elements,
sorted in ascending order.
Which one of the following statements about the function ProcessArray is CORRECT?
#include <stdio.h>
void print(int n, int j)
{
if (j ≥ n)
return;
if (n-j > 0 && n-j ≥ j)
printf("%d %d\n", j, n-j);
3
print(n, j+1);
}
int main()
{
int n = 8;
print(n, 1);
}
(a) 1 7 (b) 1 7
26 26
35 35
44 44
44
(c) 1 7 (d) 1 2
26 34
35 56
78
Explanation:
For a given number n, the program prints all distinct pairs of positive integers with sum equal to
n.
9. Which of the following is correct recurrence for worst case of Binary Search?
10. Given a sorted array of integers, what can be the minimum worst case time complexity to
find ceiling of a number x in given array? Ceiling of an element x is the smallest element present
in array which is greater than or equal to x. Ceiling is not present if x is greater than the
maximum element present in array. For example, if the given array is {12, 67, 90, 100, 300, 399}
and x = 95, then output should be 100.
4
(a) O (Log Log n) (b) O (n)
(c) O (Log n) (d) O (Log n * Log n)
Explanation:
We modify standard binary search to find ceiling. The time complexity T(n) can be written as:
T(n) ≤ T(n/2) + O(1)
Solution of above recurrence can be obtained by Master Method. It falls in case 2 of Master
Method. Solution is O (Log n).
11. Consider the following C program that attempts to locate an element x in an array Y[] using
binary search. The program is erroneous. (GATE CS 2008)
Explanation:
The above program doesn’t work for the cases where element to be searched is the last element
of Y[ ] or greater than the last element (or maximum element) in Y[ ]. For such cases, program
goes in an infinite loop because i is assigned value as k in all iterations, and i never becomes
equal to or greater than j. So while condition never becomes false.
5
12. In the above question, the correction needed in the program to make it work properly is:
(GATE CS 2008)
Explanation:
Below is the corrected function:
13. You are given a list of 5 integers and these integers are in the range from 1 to 6. There are no
duplicates in list. One of the integers is missing in the list. Which of the following expression
would give the missing number?
Let elements of list can be accessed as list[0], list[1], list[2], list[3], list[4]
Explanation:
XOR of all list elements and numbers from 1 to 6 gives the missing number.
6
14. In a village, people build houses in the same side of the road. A thief plans to loot the village.
He wants maximum amount of money without having any risk of getting caught. By some
means, the villagers know that their adjacent house is being looted or not and thus they become
alert. So the thief cannot loot contiguous two houses. Given that the thief knows the amount of
money stored in each house and the road is straight and there is no turning, which is the most
efficient algorithmic strategy to solve this problem?
15. In the above question, which entry of the array X, if TRUE, implies that there is a subset
whose elements sum to W?
Explanation:
If we get the entry X[n, W] as true then there is a subset of {a1, a2, .. an} that has sum as W.
16. A sub-sequence of a given sequence is just the given sequence with some elements (possibly
none or all) left out. We are given two sequences X[m] and Y[n] of lengths m and n respectively,
with indexes of X and Y starting from 0. We wish to find the length of the longest common sub-
sequence (LCS) of X[m] and Y[n] as l(m, n), where an incomplete recursive definition for the
function l(i, j) to compute the length of:
7
17. Consider two strings A = “qpqrr” and B = “pqprqrp”. Let x be the length of the longest
common subsequence (not necessarily contiguous) between A and B and let y be the number of
such longest common subsequences between A and B. Then x + 10y = _______.
(a) 33 (b) 23
(c) 43 (d) 34
Explanation:
The LCS is of length 4. There are 3 LCS of length 4 “qprr”, “pqrr” and “qpqr”.
19. Four matrices M1, M2, M3 and M4 of dimensions pxq, qxr, rxs and sxt respectively can be
multiplied is several ways with different number of total scalar multiplications. For example,
when multiplied as ((M1 × M2) × (M3 × M4)), the total number of multiplications is pqr + rst +
prt. When multiplied as (((M1× M2) ×M3) × M4), the total number of scalar multiplications is
pqr + prs + pst.
If p = 10, q = 100, r = 20, s = 5 and t = 80, then the number of scalar multiplications needed is
Explanation:
It is basically matrix chain multiplication problem. We get minimum number of multiplications
using ((M1 × (M2 × M3)) × M4).
8
20. The subset-sum problem is defined as follows. Given a set of n positive integers, S= {a1, a2,
a3,…, an} and positive integer W, is there a subset of S whose elements sum to W? A dynamic
program for solving this problem uses a 2-dimensional Boolean array X, with n rows and W+1
columns. X[i, j],1 ≤ i ≤ n, 0 ≤ j ≤ W, is TRUE if and only if there is a subset of {a1 ,a2 ,...,ai}
whose elements sum to j.
(a) X[i, j] = X[i – 1, j] ⋁ X[i, j -ai] (b) X[i, j] = X[i – 1, j] ⋁ X[i – 1, j – ai]
(c) X[i, j] = X[i – 1, j] ⋁ X[i, j – ai] (d) X[i, j] = X[i – 1, j] ⋁ X[i -1, j – ai]
Explanation:
X[i, j] (2 ≤ i ≤ n and ai ≤ j ≤ W), is true if any of the following is true