Explanation:
an average of $3 per student would mean
sum(all students $) / 45 = 3
sum(all students $) = 3×45 = 135
x students have $5.
therefore (45-x) students have $2.
5x + 2(45-x) = 135
5x + 90 - 2x = 135
3x = 45
x = 15
so, 15 students have $5, and 45-15 = 30 students have $2.