- Assume $f$ is surjective, and that $g_1 \circ f = g_2 \circ f$. Want to show $g_1 = g_2$
Assume $g_1 \neq g_2$. Then, there is some $y \in Y$ so that $g_1(y) = z_1 \neq z_2 = g_2(y)$. Since $f$ is surjective, there is an $x \in X$ so that $f(x) = y$. What does this tell you about $g_1(f(x))$ and $g_2(f(x))$?
- Assume $g_1 \circ f = g_2 \circ f$ implies $g_1 = g_2$ for any $g_1,g_2$. Want to show $f$ is surjective.
Assume $f$ is not surjective. Let $A = \textrm{Im}(f) = \{ f(x) : x \in X\} \subset Y$. Because $f$ is not surjective, $Y \backslash A$ is non-empty. Choose $g_1,g_2$ so that $g_1(y) = g_2(y)$ for all $y \in A$, but $g_1 \neq g_2$ for some $y \in Y \backslash A \neq \emptyset$. Then $g_1 \neq g_2$, but $g_1\circ f = g_2 \circ f$, and you have a contradiction.
(Implicit in this is that $Z$ consists of more than one point, otherwise such $g_1,g_2$ cannot be chosen)