Connection Status:
Competition Arena > Sets
SRM 159 · 2003-08-12 · by dimkadimon · Math
Class Name: Sets
Return Type: int[]
Method Name: operate
Arg Types: (vector<int>, vector<int>, string)
Problem Statement

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 int[]s representing sets A and B, and an operation applied on them, return a int[] representing the resulting set sorted in ascending order. If the result is an empty set then return an empty int[]. operation will be one of the following: "UNION", "INTERSECTION", "SYMMETRIC DIFFERENCE".

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".
Examples
0)
{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)
{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.

2)
{432,756,123}
{534,76,1209}
"INTERSECTION"
Returns: { }

There are no common elements, so we must return an empty set.

3)
{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.

4)
{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.

Coding Area

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

Submitting as anonymous