Open In App

Algorithms | Misc | Question 13

Like Article
Like
Save
Share
Report

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

 

Consider the following algorithm in which x,y, and z are Boolean arrays of size n: 

C




algorithm zzz(x[] , y[], z [])
{
   int i;
   for (i=O; i<n; ++i)
     z[i] = (x[i] ^ ~y[i]) V (~x[i] ^ y[i]) }


The set Z computed by the algorithm is:

(A)

(X Intersection Y)

(B)

(X Union Y)

(C)

(X-Y) Intersection (Y-X)

(D)

(X-Y) Union (Y-X)



Answer: (D)

Explanation:

The expression x[i] ^ ~y[i]) results the only 1s in x where corresponding entry in y is 0. An array with these set bits represents set X – Y The expression ~x[i] ^ y[i]) results the only 1s in y where corresponding entry in x is 0. An array with these set bits represents set Y – X. The operator “V” results in Union of the above two sets.


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads