Code for calculating number of variations
Similar to the other question, I would suggest to utilize a std::map changing your loadTestCases accordingly

std::map&lt;int, int&gt;loadTestCases(int N){
    std::map&lt;int, int&gt; testCases;
    int TestCase;
    for (int i = 0; i &lt; N; i++)
    {
        std::cin &gt;&gt; TestCase;
        auto inserted = testCases.emplace(TestCase, 1);
        if(!inserted.second) {
            testCases.at(TestCase)++;
        }
    }
    return testCases;
}


The advantage of this approach is, that you only have to traverse the keys of the std::map once and can multiply the values if there is a match.

Also given that your container is sorted you can actually skip the std::abs call, as a_i - a_j > 0 is guaranteed by that sort, if you traverse the container from the back. Also more importantly, once you find a_i - a_l > k, you automatically know, that any following pair is also valid, as by construction for an a_m &lt; a_l we get a_i - a_m = a_i - a_l - (a_m - a_l), with (a_m - a_l) &lt; 0.