CJCoding With Joseph
15free / daySign inCreate account

Number Set Range Queries

Build a class NumberSet that stores distinct integers in a set<int> and answers range queries. It must provide these public methods:

1. void add(int x): inserts x. Adding a value that is already present changes nothing.
2. void remove(int x): erases x if it is present; does nothing if x is not in the set.
3. int countBetween(int lo, int hi) const: returns how many stored values v satisfy lo <= v <= hi (both ends inclusive). Returns 0 if none do, and also returns 0 when lo > hi.
4. int closestAbove(int x) const: returns the smallest stored value that is strictly greater than x, or -1 if there is none.

The test prints each query result on its own line. After adding 10, 20, 30, 40, 50 and removing 20, the calls countBetween(10, 40), closestAbove(40) and closestAbove(50) give the output shown in Expected Output.

Do NOT write a main function.

Expected Output:

3
50
-1
0
1
0
5
-1

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
STL ContainersSets
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.