Skip to content
Official PYQ · 2006

GATE CS 2006 Q50 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

A set XX can be represented by an array x[n]x[n] as follows: 

 x[i]={1if i∈X0otherwisex\left [ i \right ]=\begin {cases} 1 & \text{if } i \in X \\ 0 & \text{otherwise} \end{cases}

Consider the following algorithm in which xx, yy, and zz are Boolean arrays of size nn: 

algorithm zzz(x[], y[], z[]) {
int i;

for(i=0; i<n; ++i)
z[i] = (x[i] ∧ ~y[i]) ∨ (~x[i] ∧ y[i]);
}

The set ZZ computed by the algorithm is: 

Choose one option
View worked solution

Answer: D

The algorithm uses characteristic arrays xx and yy to represent sets XX and YY respectively, where a value of 11 at index ii indicates that ii is a member of the set, and 00 indicates it is not.

For each index ii, the algorithm computes:

z[i]=(x[i]∧¬y[i])∨(¬x[i]∧y[i])\text{z}[i] = (x[i] \land \neg y[i]) \lor (\neg x[i] \land y[i])

Let us translate this logical expression into set operations:

  • x[i]∧¬y[i]x[i] \land \neg y[i] is true if and only if i∈Xi \in X and i∉Yi \notin Y. This corresponds to the set difference: X−YX - Y.
  • ¬x[i]∧y[i]\neg x[i] \land y[i] is true if and only if i∉Xi \notin X and i∈Yi \in Y. This corresponds to the set difference: Y−XY - X.
  • The logical OR (∨\lor) operator combines these two conditions, which corresponds to the set union (∪\cup).

Thus, z[i]=1z[i] = 1 if and only if:

i∈(X−Y)∪(Y−X)i \in (X - Y) \cup (Y - X)

This expression represents the symmetric difference of the two sets, XΔYX \Delta Y. Therefore, the set ZZ computed by the algorithm is (X−Y)∪(Y−X)(X - Y) \cup (Y - X).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions