Your logic thinking is great. Your solution is my first thought. However, if you like to find out the best algorithm, calculation is cheaper than logic comparison.