Disclaimer: This question will use as an example a computer-science university program, because it is much easier for me to describe and give some statistics, but it applies to math as well. The reason I posted it here is that talent (i.e. mathematical gift) plays a much greater role in thinking-heavy areas than in, for example, biology or history.
Context:
The university welcomes all kinds of students with varying degree of mathematical/programming ability. They are split into groups of 15 (approximately), however, to teach the beginners programming and to keep the experienced engaged, each such group might be "imperative" (beginners learn some standard programming language) or "functional" (those who could code before learn some functional language). There is a test of their programming skill and then students are assigned accordingly to the results (if student wishes and there are no obstacles, he may apply for a change; in the past there was no test and the students could pick the group type as they wished).
Summarizing: there are several "imperative" groups which contain mostly beginners and a few "functional" groups which include students that could code before. It is worth noting that the majority of the gifted students would usually participate in programming contests in high school, while the average and below-average students almost always can't code (even if they know the syntax, the test is too hard for them).
This works quite well: with such a split the beginners learn the basics, while students from "functional" groups are engaged because of the more advanced topics.
This works quite well, until the exams. The groups motivate/demotivate themselves internally and in result mediocre students from the "functional" groups have learned a way more than the good from the "imperative" groups. It wouldn't be a big problem, because the results between "functional" and "imperative" groups are incomparable - the tests are of incomparable difficulty, hence are graded separately.
However, as the schedules are inter-dependent, the groups formed there are kept also for other courses, i.e. these which didn't plan for a split at all. Again, groups motivate themselves internally (and also teachers are more engaged with brighter students), but this time all the students are graded according to the same key. The gap in performance is disastrous: the average score for the first group might be easily over $80\%$, while the average for the rest can be as low as $25\%$. Such results have impact on their learning, motivation and many other things, but most importantly, a big part that would attend the "imperative" would fail their exams and sometimes even drop out.
One could think that splitting the students by abilities is just a bad solution. On the other hand, their knowledge and experience varies so much that it would be impossible to keep all the parties interested. For example, this happens for electable courses, where the groups mix together, and pose a serious challenge for the teachers. It is so inconvenient that professors happen to make their optional classes intentionally very hard or very easy (the funny part is that it even feels natural, because those courses are in majority theoretical and practical respectively).
I'm sure that it's not a healthy state of affairs, but this is a hard problem and might lack a general solution. Also, there is the whole topic of sliding scale between teaching all something and teaching some everything, but that's out of scope of this question.
Question: Is there a way to avoid the negative effects of the split, in particular effects of good students from "imperative" groups averaging down and poor students from "functional" groups averaging up?