Day 2: The “Two Sum” Triumph and the Thrill of Optimization

Hello Pythonistas, welcome back!
This is day 2 of 5 days of Array.
Today, I solved the problem of “Two Sum,” and it was a see-it-yourself.

My first thought was to go with a brute-force approach.

But isn’t that just too easy? Optimization is where the real trouble lies.

Though it seems like a basic question, but optimizing for it completely took me by surprise.

I dived straight into sorting out the array assuming that that would be the key.

But nope, that only messed up the indexes and led to my solution going haywire.

Then again seemed promising but since the array is not sorted out It would also not have been possible. At this point, frustration began setting in and then I looked up for the solution.

Remember sometimes you may need to peek at solutions and there is no shame in doing so. After all, we are here to learn right?

Thus, I strategically glanced at an optimized solution which was a hash map approach.

Like someone had thrown on a light switch, everything fell into place for me with regard to this particular problem.

The hash map gave another angle on the way of solving the task more effectively.

Today’s Notes

Leave a Reply