Maximum value in the Gruss inequality The Gruss inequality states:
$$\left|\frac{1}{x_2-x_1}\int_{x_1}^{x_2}dxf(x)g(x)-\frac{1}{(x_2-x_1)^2}\int_{x_1}^{x_2}dxf(x)\int_{x_1}^{x_2}dxg(x)\right|\le\frac{1}{4}(A-a)(B-b)$$
where $a\le f(x)\le A$  , $b\le g(x)\le B$ in $x\in [x_1,x_2]$
My question is: when the sign of equal holds?
 A: 
Claim: The inequality becomes equality iff there exists a measurable set $E\subset[x_1,x_2]$, such that $|E|=\frac{x_2-x_1}{2}$,
  $f=a+(A-a)\chi_E$ a.e. and $g=b+(B-b)\chi_E$ or $g=B-(B-b)\chi_E$ a.e. on  $[x_1,x_2]$. 

Sketch of proof: Let me borrow the argument, results and notations in this paper.
To begin with, note that we may focus on the special case that $[x_1,x_2]=[0,1]$ and $0\le f,g\le 1$. Then the original inequality is reduced to
$$|\int fg-\int f \int g|\le \frac{1}{4},$$
and by replacing $f$ with $1-f$ if necessary, we may suppose that $\int fg\ge\int f \int g$, so we only need to check when equality holds in
$$\int fg-\int f \int g\le \frac{1}{4}.\tag{1}$$
To complete the proof, it suffices to show that there exists a measurable set $E\subset[0,1]$, such that equality in $(1)$ holds iff $|E|=\frac{1}{2}$ and $f=g=\chi_E$ a.e. on $[0,1]$. (The situation $\int fg\le\int f \int g$ corresponds to $f=\chi_E$ and $g=1-\chi_E$.)
Following the paper, let $u=\max\{f,g\}$, $w=\min\{f,g\}$, $F=\{f\ge g\}$ and $G=\{f\le g\}$. Then
$$\int fg-\int f\int g\le\int fg-\int f\int g+\int_F|f-g|\int_G|f-g|=\int uw-\int u\int w.\tag{2}$$ 
Since $0\le w\le u\le 1$, $\int w\in[0,1]$ and $x(1-x)\le\frac{1}{4}$ on $[0,1]$,
$$\int uw-\int u\int w \le \int w-(\int w)^2\le\frac{1}{4}.\tag{3}$$
In $(3)$, the second inequality becomes equality iff $\int w=\frac{1}{2}$; provided that $\int w>0$, the first inequality becomes equality iff 
$$\int uw=\int w \iff u=1 \text{ on }\{w>0\} \quad\text{and}\quad \int u=\int w \iff u=w\ a.e.$$ 
Therefore, the first inequality becomes equality iff $f=g=\chi_E$ a.e. and the second inequality becomes equality iff $|E|=\frac{1}{2}$.
Once $u=w$ a.e., the inequality in $(2)$ becomes equality, which completes the proof.
