Optimized Problem 9 by using std:set, which is implemented as a binary search tree for fast search