A Clockwork Orange is supposedly loyal to it's source material, the American edition of the novel.
> This plot discrepancy occurred because Kubrick based his screenplay upon the novel's American edition, its final chapter deleted on insistence of the American publisher.
I can appreciate that, but to me it feels like a deux ex machina given how the events unfolded up until the end. I think it demonstrates a cultural difference between Americans and British- by taking the route of personal growth, the book goes against the dour, nihilistic ending British would expect. I don't remember any buildup to Alex's epiphany, so I found it unsatisfying.
They both have happy endings. In the book, the final chapter is about Alex starting to mature into an adult.
The book is also organized into three sections of seven chapters meant to represent the three phases of childhood and ending in chapter 21: adulthood. The movie was pretty close to the book it just left off the 21st chapter as did the American version of the book.
I had to study the novel in high school as you mentioned the 21 chapter thing is important because the book is divided into 3 parts of 7 chapters and there is supposed to be an element of symmetry in the progression.
One of the central themes running through the book is free will and choice. It has been a fair amount of time now since I've read it but I believe the book opens with the quote "So what is it going to be then?" and ends with Alex making the choice to set aside his past. Importantly he also says something along the lines that if he ever has children he won't try to stop them making the same choices as he has.
One of the things our teacher repeatedly stressed was Saint Augustine and Anthony Burgess's Catholic background.
Imagine A Clockwork Orange having a happy ending... Well, that's how the book ends.