Write a function to solve a specific algorithmic problem, and explain the time and space complexity of your solution.