Improved the problem statement and simplified the implementation.