Following questions have been asked in GATE CS 2008 exam.

**1. 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. Which of the following is valid for 2 <= i <= n and ai <= j <= W?**

(A) X[i, j] = X[i – 1, j] V X[i, j -ai]

(B) X[i, j] = X[i – 1, j] V X[i – 1, j – ai]

(C) X[i, j] = X[i – 1, j] V X[i, j – ai]

(D) X[i, j] = X[i – 1, j] V X[i -1, j – ai]

Answer (B)

X[I, j] (2 <= i <= n and ai <= j <= W), is true if any of the following is true
1) Sum of weights excluding ai is equal to j, i.e., if X[i-1, j] is true.
2) Sum of weights including ai is equal to j, i.e., if X[i-1, j-ai] is true so that we get (j – ai) + ai as j.
**2. In question 1, which entry of the array X, if TRUE, implies that there is a subset whose elements sum to W?**

(A) X[1, W]

(B) X[n ,0]

(C) X[n, W]

(D) X[n -1, n]

Answer (C)

If we get the entry X[n, W] as true then there is a subset of {a1, a2, .. an} that has sum as W.

Reference: http://en.wikipedia.org/wiki/Subset_sum_problem

**3. Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.**

`1. f(` `int` `Y[10], ` `int` `x) { ` `2. ` `int` `i, j, k; ` `3. i = 0; j = 9; ` `4. ` `do` `{ ` `5. k = (i + j) /2; ` `6. ` `if` `( Y[k] < x) i = k; ` `else` `j = k; ` `7. } ` `while` `(Y[k] != x && i < j); ` `8. ` `if` `(Y[k] == x) ` `printf` `(` `"x is in the array "` `) ; ` `9. ` `else` `printf` `(` `" x is not in the array "` `) ; ` `10. }` |

**On which of the following contents of Y and x does the program fail?**

(A) Y is [1 2 3 4 5 6 7 8 9 10] and x < 10
(B) Y is [1 3 5 7 9 11 13 15 17 19] and x < 1
(C) Y is [2 2 2 2 2 2 2 2 2 2] and x > 2

(D) Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even
Answer (C)
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.

4. In question 3, the correction needed in the program to make it work properly is

(A) Change line 6 to: if (Y[k] < x) i = k + 1; else j = k-1;
(B) Change line 6 to: if (Y[k] < x) i = k - 1; else j = k+1;
(C) Change line 6 to: if (Y[k] <= x) i = k; else j = k;
(D) Change line 7 to: } while ((Y[k] == x) && (i < j));
Answer (A)
Below is the corrected function

`f(` `int` `Y[10], ` `int` `x) { ` ` ` `int` `i, j, k; ` ` ` `i = 0; j = 9; ` ` ` `do` `{ ` ` ` `k = (i + j) /2; ` ` ` `if` `( Y[k] < x) i = k + 1; ` `else` `j = k - 1; ` ` ` `} ` `while` `(Y[k] != x && i < j); ` ` ` `if` `(Y[k] == x) ` `printf` `(` `"x is in the array "` `) ; ` ` ` `else` `printf` `(` `" x is not in the array "` `) ; ` `} ` |

Reference: http://en.wikipedia.org/wiki/Binary_search_algorithm#Implementations

**Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.**

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.