Answer: 24n(n2−1) if n is odd and 24n(n2−4) if n is even.
Let ai be the number of students that student i loves (1≤i≤n). Then the number of love triangles is
T=(3n)−(2a1)−⋯−(2an).
This is explained as follows. In a non-love triangle ABC, one student, say A, loves B and C, while B loves C. For each pair B, C of students that A loves, one triangle is thus deducted above from the total number of triangles.
Simplifying the above expression, we get
T=(3n)+21(a1+⋯+an)−21(a12+⋯+an2).
We have (3n)=6n(n−1)(n−2) and a1+⋯+an=(2n)=2n(n−1), while the sum of squares is bounded by Chebyshev's Inequality:
na12+⋯+an2≥(na1+⋯+an)2=n21(2n)2=4(n−1)2,
yielding
a12+⋯+an2≥4n(n−1)2.
Hence an upper bound for the number of love triangles is given by
T≤6n(n−1)(n−2)+21⋅2n(n−1)−214n(n−1)2=24n(n2−1).
Equality holds if and only if a1=⋯=an=2n−1. This can easily be arranged when n is odd, by making student i fall in love with students i+1,…,i+2n−1 (counting cyclically).
When n is even, this bound is clearly impossible to attain, and we must proceed differently. We investigate, for n even, the minimal value of a12+⋯+an2, subject to the condition a1+⋯+an=(2n).
Suppose first the sum of squares is at a minimum, but that ap−aq≥2 for some p and q. We may then replace ap and aq by ap−1 and aq+1, respectively, which will serve to decrease the sum of squares:
(ap−1)2+(aq+1)2=ap2+aq2+2(1+aq−ap)<ap2+aq2.
Consequently, when the sum of squares is at a minimum, the maximal difference among the numbers ai is at most 1.
Suppose next, without loss of generality, that a1=⋯=ak=x and ak+1=⋯=an=x+1. Then
(2n)=a1+⋯+an=kx+(n−k)(x+1)=nx+n−k⇔k=nx+n−(2n),
transforming the inequality 0≤k≤n into
0≤nx+n−(2n)≤n⇔2n−1−1≤x≤2n−1⇔x=2n−2.
This corresponds to k=2n, and so the minimum is attained when half of the ai equal 2n−2 and the remaining half equal 2n. The minimal quadratic sum is
2n(2n−2)2+2n(2n)2=4n(n2−2n+2).
We thus find
T≤6n(n−1)(n−2)+21⋅2n(n−1)−21⋅4n(n2−2n+2)=24n(n2−4)