Activity Selection Problem

I sorted activities by their finishing time, and select next activity whose starting more than finishing time of last selected one.
The code is working fine, here I just made a guess that this might work, I want to know why and how exactly this worked.

@sarthakj2099,
Because, if you want to do maximum number of activities, then you would want to reduce your idle time, thus start with other activity as soon as possible after ending an activity. But, you can’t do more than one activity at ones, so you choose an activity with start time closest to the end time of the activity you are currently doing.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.