It is never okay to end an interview early because the candidate wasn't good enough. This is immensely disrespectful, especially for an onsite interview where the candidate carved out an entire day to do it (likely taking PTO if they are currently working).
As an interviewer, you are also representing your company. If you do this, you are hurting your company's reputation as people talk - That candidate is very likely to tell others that your company is a horrible place to interview at.
When this scenario happens, the proper thing to do is to switch gears and go entirely into maximizing candidate experience (as your goal isn't collecting signal anymore). This involves:
Giving them hints aggressively to help them complete the problem
Pair programming with them and trying to teach them - Use your interview to make the candidate a better software engineer!
Allocating more time for them to ask questions about the company - Try to get them excited! If you do this properly, they'll want to interview with your company again at a later time. People change - They may come back in a few years and be an absolutely incredible engineer you 100% want to hire.