Wow!! Thank you very much for finding this bug. I cannot believe that you found this bug. You see, I maintain a note for all problems I solve. In the notes, the recursion that I wrote was present. But then, I checked my code and voila, the recursion was different! I believe that I had encountered this exact bug and later forgot to update my notes.
The bug in my answer was that we never considered that the previous element alone could be a starting point! In my code, I checked for the singular length segment as a special case and then did the recursion.
So finally, we get the algorithm as: