The editorial’s approach is rather complicated, I could not understand much either. Maybe I’ll sit with it later and try to figure it out, but here is my solution for now.

We have to find triples of (i, j, k), i < j < k such that j/i=k/j (1-indexed). Now let’s say that k/j=j/i=p/q, where p/q is the fraction in its lowest terms. Because k is an integer and i\times p^2/q^2=k, i must be divisible by q^2! So for each i I try to generate values q such that q^2 divides i. The next step is to generate values for p, with p>q. If k=i \times p^2/q^2 is within the array bounds and the S values at (i,\; i \times p/q,\; i \times p^2/q^2) have values (‘a’, ‘b’, ‘c’) or (‘c’, ‘b’, ‘a’), then we have a satisfactory triple. The only catch is that sometimes duplicate triples will be generated since some p/q obtained will be reducible. To deal with this we can use gcd to check whether p/q can be reduced further, and only increment the answer counter when it cannot.

Here is the link to my solution: link.

There is also another solution given by *kilicars* in the “Discussions” tab which appears simple enough. Hope this helps

**NOTE**: Updated the answer, checking divisibility of i by q^2 now instead of q, don’t know why I didn’t think of this earlier. It’s much faster now.