Here is Prob. 24, Chap. 4 in the book Principles of Mathematical Analysis by Walter Rudin, 3rd edition:
Assume that $f$ is a continuous real function defined in $(a, b)$ such that $$ f \left( \frac{ x+y }{2} \right) \leq \frac{ f(x) + f(y) }{2} \ \tag{0} $$ for all $x, y \in (a, b)$. Prove that $f$ is convex.
My effort:
We need to show that $$f \left( (1-\lambda) x + \lambda y \right) \leq (1-\lambda) f(x) + \lambda f(y) \ \mbox{ for all $x, y \in (a, b)$ and for all $\lambda \in [0, 1]$ }. \ \tag{1} $$
First, we show that (1) holds for all numbers $\lambda$ of the form $$\lambda \colon= \frac{k}{2^n} \ \mbox{ for all } \ n = 0, 1, 2, 3, \ldots \ \mbox{ and for all integers } \ k \mbox{ such that } \ 0 \leq k \leq 2^n. \ \tag{2} $$
For $n= 0$, we can have either $k= 0$ or $k=1$, and so $$\mbox{ either } \ \lambda = 0 \ \mbox{ or } \ \lambda = 1,$$ and (1) holds trivially in either one of these two cases.
For $n=1$, we see that $k$ can be $0$, $1$, or $2$, and then $\lambda$ is $0$, $\frac{1}{2}$, or $1$. For the first and the last cases, the inequality (1) holds trivially as we have discussed above, while for $\lambda = \frac{1}{2}$, (1) follows from (0) in the hypothesis of this problem.
Let $n$ be a natural number such that $n \geq 2$, and assume that (1) holds for all natural numbers $i$ such that $i < n$.
Now in (2) if $k$ is a non-negative even integer, then we can write $k = 2m$ for some non-negative integer $m$ and so $\lambda = \frac{m}{2^{n-1}},$ which implies by our induction hypothesis that (1) holds for this $\lambda$.
So we assume that in (2), $k$ is an odd (positive) integer. Then $k$ is an integer such that $$1\leq k \leq 2^n - 1,$$ and thence $\frac{k\pm 1}{2}$ are also integers such that $$0 \leq \frac{k-1}{2} \leq 2^{n-1} - 1 \ \ \mbox{ and } \ \ 1 \leq \frac{k+1}{2} \leq 2^{n-1}. \ \tag{3} $$ Now let $$ s \colon = \frac{ \frac{k-1}{2}}{2^{n-1}} \ \ \mbox{ and } \ \ t \colon= \frac{ \frac{k+1}{2}}{2^{n-1}}. $$ Then by (3) we can conclude that $s$ and $t$ are in a range for which (1) holds by our induction hypothesis. That is, $$f\left( (1-s)x + s y \right) \leq (1-s)f(x) + s f(y) \ \mbox{ and } \ f\left( (1-t)x + t y \right) \leq (1-t)f(x) + t f(y) \ \mbox{ for all } \ x, y \in (a, b). \ \tag{4} $$
Moreover we note that $$\frac{s+t}{2} = \frac{ \frac{ \frac{k-1}{2}}{2^{n-1}} + \frac{ \frac{k+1}{2}}{2^{n-1}} }{2} = \frac{k}{2^n} = \lambda, \ \tag{5} $$ and so for all $x, y \in (a, b)$, we see that $$ \begin{align} f\left( (1-\lambda) x+ \lambda y \right) &= f \left( \left( 1- \frac{s+t}{2} \right) x + \frac{s+t}{2} y \right) \ \mbox{ using (5) above } \\ &= f \left( \frac{ \left( (1-s)x + sy \right) + \left( (1-t) x + ty \right)}{2} \right) \\ &\leq \frac{ f\left( (1-s)x + sy \right) + f\left( (1-t) x + ty \right)}{2} \ \mbox{ by (0) above } \\ &\leq \frac{ \left( (1-s)f(x) + s f(y) \right) + \left( (1-t) f(x) + t f(y) \right)}{2} \ \mbox{ using (4) above } \\ &= \left( 1 - \frac{s+t}{2} \right) f(x) + \frac{s+t}{2} f(y) \\ &= (1-\lambda) f(x) + \lambda f(y) \ \mbox{ by \tag{5} above}. \end{align} $$ Thus we have shown that (1) holds for all real numbers $\lambda$ of the form in (2) above. Am I right?
Now let $x, y \in (a, b)$ be arbitrary, and let $g$ be the mapping of $\mathbb{R}^1$ into $\mathbb{R}^1$ defined by the equation $$ g(\lambda) = (1-\lambda) f(x) + \lambda f(y) - f\left( (1-\lambda) x+ \lambda y \right) \ \mbox{ for all } \ \lambda \in \mathbb{R}.$$ Then since $f$ is continuous in $(a, b)$, therefore $g$ is continuous on $[0, 1]$.
Moreover all real numbers $\lambda$ of the form (2) are in the inverse image under $g$ of the infinite closed interval $[0, +\infty)$, and this interval is a closed set in $\mathbb{R}^1$. This due to the continuity of $g$ on $[0, 1]$ implies that the inverse image under $g$ of $[0, +\infty)$ is also a closed set in $[0, 1]$ and hence a closed set in $\mathbb{R}^1$, because $[0, 1]$ is itself a closed set in $\mathbb{R}^1$. Am I right?
What next? How to complete this proof from here?
Is the continuity of $f$ essential for the conclusion to hold?