Leetcode: Two-sum an intuitive approach.

Carole Sabino
Women in Technology
4 min readApr 24, 2024

--

Photo by David Klein on Unsplash

Imagine yourself as the proud owner of a souvenir shop inside the Spot-On Change Hotel. When closing the cash register, you notice an excess of 8 euros. It seems that a guest didn’t receive the exact change. This could tarnish the hotel’s reputation. To avoid this, you decide to solve the mystery of the incorrect change. Upon opening the shop’s cash system, you discover that the error occurred on two different accounts!

You devise a plan: to visit each room and ask the guests what change they received in the shop until you find the two with the wrong bill. Arriving on the first floor, a guest reports receiving 4 euros in change. You calculate that you need to find a number that, when added to the 4 euros, results in 8. Moving to the second floor, the guest’s change is 5 euros. 4 + 5 results in 9, so it can’t be this one… Fifth floor, sixth… Tenth, NO, NO, NO!

Since you didn’t find the value on the first attempt, you go down to the second floor and start asking all the guests again about their change so that you can compare it with the value from the next starting floor, exhausting, isn’t it? In computer science, this search method is called brute force, an extremely slow search method that works by comparing one item with the following ones in the array.

--

--