TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

SetFunctions

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given 2 int[]s representing sets of integers. In addition, you will be given a String op that determines the operation you will perform on the 2 given sets. op can be one of (quotes for clarity):
  • "UNION" : Return a list of the integers that occur in either of the two sets or both.
  • "SYMDIFF" : Return a list of the integer that occur in either of the two sets but not both.
  • "INTERSECTION" : Return a list of the integers that occur in both of the two sets.
  • "MINUS" : Return a list of the integers that occur in set1 but not in set2.
Before returning the resultant int[] remove all duplicates, and sort the list into increasing order. See examples for further information.

Definition

Class:
SetFunctions
Method:
operate
Parameters:
int[], int[], String
Returns:
int[]
Method signature:
int[] operate(int[] set1, int[] set2, String op)
(be sure your method is public)

Constraints

  • set1 will contain between 0 and 50 elements inclusive.
  • set2 will contain between 0 and 50 elements inclusive.
  • Each element in set1 will be between -1000 and 1000 inclusive.
  • Each element in set2 will be between -1000 and 1000 inclusive.
  • Each element in set1 will be distinct.
  • Each element in set2 will be distinct.
  • op will be one of (quotes for clarity): "UNION", "SYMDIFF", "INTERSECTION", or "MINUS"

Examples

  1. {1,2,3,4,5}
    {3,4,5,6,7}
    "UNION"
    Returns: { 1,  2,  3,  4,  5,  6,  7 }
    The elements can come from either set or both
  2. {1,2,3,4,5}
    {3,4,5,6,7}
    "SYMDIFF"
    Returns: { 1,  2,  6,  7 }
    The elements can come from either set, but not both
  3. {1,2,3,4,5}
    {3,4,5,6,7}
    "INTERSECTION"
    Returns: { 3,  4,  5 }
    The elements must be in both sets
  4. {1,2,3,4,5}
    {3,4,5,6,7}
    "MINUS"
    Returns: { 1,  2 }
    The elements must be in set1 but not in set2
  5. {}
    {1,2,3}
    "UNION"
    Returns: { 1,  2,  3 }
  6. {1,2,3}
    {4,5,6}
    "INTERSECTION"
    Returns: { }
  7. {1,2,3}
    {1,2,3}
    "SYMDIFF"
    Returns: { }
Back to all problems