OTT - Editorial

Problem Link - CodeChef: Practical coding for everyone
Author - amarnathsama
Tester - amarnathsama
Editorilist - apurv001

IDEA
The number of pairs that satisfy the first condition is 123, where 1, 2, 3 are the numbers of 1, 2, and 3 contained in S, respectively.
is.
Consider how many of these pairs do not meet the second condition. Like satisfying j −i = k −j
The number of pairs (i, j, k) is O(N^2). Therefore, all of this is adjusted by, for example, fixing i and j.
All you have to do is make sure it meets the first condition. The computational complexity of this algorithm is
The second half of the full search becomes the bottleneck and is O(N^2).

Summary
#include <bits/stdc++.h>
using namespace std;
signed main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int r = count(s.begin(), s.end(), '1');
    int b = count(s.begin(), s.end(), '2');
    int g = count(s.begin(), s.end(), '3');
    ll ans = r * (ll)b * g;
    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++)
        {
            int k = 2 * j - i;
            if (k >= n)continue;

            if (s[i] != s[k] && s[i] != s[j] && s[j] != s[k])
            ans--;
        }
    }
    cout << ans << endl;
}