Sets
SRM 159 · 2003-08-12 · by dimkadimon
Problem Statement
A set of numbers is a collection of numbers with no repeated elements. We can define the following set operations:
The UNION of two sets A and B is a set containing all the elements that are either in A or in B.
The INTERSECTION of two sets A and B is a set containing all the elements that are in both A and B.
The SYMMETRIC DIFFERENCE of two sets A and B is a set containing all the elements that are either in A or in B, but not containing elements that are in both A and B.
Given two
Constraints
- A will have between 0 and 50 elements inclusive.
- B will have between 0 and 50 elements inclusive.
- each element in A will be between -1000000 and 1000000 inclusive.
- each element in B will be between -1000000 and 1000000 inclusive.
- A will not have any repeated elements.
- B will not have any repeated elements.
- operation will be one of the following: "UNION", "INTERSECTION", "SYMMETRIC DIFFERENCE".
{1,2,3,4}
{3,4,5,6}
"INTERSECTION"
Returns: { 3, 4 }
The only elements that are both in A and in B are 3 and 4.
{1,2,3,4}
{3,4,5,6}
"UNION"
Returns: { 1, 2, 3, 4, 5, 6 }
Here we return all the elements that are either in A or in B.
{432,756,123}
{534,76,1209}
"INTERSECTION"
Returns: { }
There are no common elements, so we must return an empty set.
{6,5,7,4}
{7,6,4,10}
"SYMMETRIC DIFFERENCE"
Returns: { 5, 10 }
Elements 4, 6, 7 are in both sets, thus they cannot be in our answer. However we can include elements 5 and 10.
{342,654,897,312,76,23,78}
{21,43,87,98,23,756,897,234,645,876,123}
"SYMMETRIC DIFFERENCE"
Returns: { 21, 43, 76, 78, 87, 98, 123, 234, 312, 342, 645, 654, 756, 876 }
Submissions are judged against all 40 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Sets with a public method vector<int> operate(vector<int> A, vector<int> B, string operation) · 40 test cases · 2 s / 256 MB per case