How many distinct functions $f : \{1, 2, 3, 4, 5 \} \to \{1, 2, 3 \}$ are there, from the set $\{1, 2, 3, 4, 5\}$ to the set $\{1, 2, 3\}$, whose range is a set of size exactly $2$?
I got $3^5$ total number of functions but don't know how to go further?
We first need to decide which elements comprise the function's range. There are $\binom32=3$ ways of doing so.
Then for each element in the domain of the function, we need to decide whether it maps to the smaller or larger element in that chosen range. There are $2^5=32$ ways of doing so, but two of them map all elements in the domain to only one element in the codomain, so are excluded. Thus there are 30 possible functions for each choice of range, and 90 functions in all satisfying the given conditions.