A set can be represented by an array as follows:
Consider the following algorithm in which , , and are Boolean arrays of size :
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 computed by the algorithm is:
View worked solution
Answer: D
The algorithm uses characteristic arrays and to represent sets and respectively, where a value of at index indicates that is a member of the set, and indicates it is not.
For each index , the algorithm computes:
Let us translate this logical expression into set operations:
- is true if and only if and . This corresponds to the set difference: .
- is true if and only if and . This corresponds to the set difference: .
- The logical OR () operator combines these two conditions, which corresponds to the set union ().
Thus, if and only if:
This expression represents the symmetric difference of the two sets, . Therefore, the set computed by the algorithm is .
Free practice · Your answers on this page are not saved.
