Three different sessions: math, biology, and history.
Let T be the total number of students
For the math session, the students are divided so that one group has 4 students and all other groups have 5 students each.
=> T = 5a + 4 (a>=1)
For the biology session, they are divided so that one group has 4 students and all other groups have 6 students each.
=> T = 6b + 4 (b>=1)
For the history session, the same students are divided so that one group has fewer than 7 students and all other groups have 7 students each.
=> T = 7c + d (d < 7 and c>=1)
Now we have 2 equations,
T = 5a + 4
T = 6b + 4
=> T ≡4 mod 5 and T ≡4 mod 6
=> T - 4 must be divided by LCM(5,6) = 30
Thus,
T = 30k + 4 for an integer k
From equation 3, and above condition we need to find k where T when divided by 7 gives remainder (d) < 7
Lets try k = 1.
T = 30*1 + 4 = 34
34 divided by 7 gives a remainder 6
=> Groups of 7 = 4 and remainder students = 6
If we were to tally this with the other equations,
For Maths ; 5a + 4 = 34 => a = 6
For Biology; 6b + 4 = 34 => b = 5
Both are integers and satisfy above a and b constraint so its valid
Groups of 7 = 4
Remaining students = 6