For any algorithm you implement, analyze its time and space complexity using Big-O notation. Derive and justify the best, average, and worst-case complexities, identify the dominant operations that drive the cost, and explain how the complexity scales with input size and constraints. Compare at least one alternative approach, discuss the trade-offs, and state any optimizations you would apply to improve asymptotic or constant factors.