HackerRank - Non-Divisible Subset
I'm not sure about what makes your code slow, but first I should point out some ways to improve its readability. Actually, it should help you to think about your code complexity to make it more readable, since performance in complex nested loops and such things is really difficult to evaluate.

To begin with a detail, I believe that:

std::for_each(v.begin(), v.end(), [&amp;k](auto&amp; x){x%=k;});


isn't the most readable. When you iterate over the whole container, the "range-based" for is better:

for (auto&amp; x : v) x %= k;


unless you want to make your intent yet more apparent with the transform algorithm:

std::transform(vec.begin(), vec.end(), vec.begin(), [k](auto&amp;&amp; i) {return i%k;});


More importantly, I think you should dissociate traversal and counting, because it makes for a code very difficult to read. Why don't you make it in two steps:

// first step: counting modulo populations
std::map&lt;int, int&gt; counter;
for (auto elem: v) ++counter[v%k];

// second step: traversing different modulos
auto no_duplicates = std::unique(vec.begin(), vec.end());
std::for_each(vec.begin, no_duplicates, [](auto etc) { do_something(etc); });


A last point, you can put the two special cases out of the way early, instead of dragging them along until the end. So deal with multiples of k and k/2 at the first occasion, it also allows you to get rid of a certain number of elements.

To sum it up, here's my suggestion:

int nonDivisibleSubset(int k, std::vector&lt;int&gt; vec) {
    // 1. check for multiples of k, multiples of half of k, and retain only one of each
    int res = 0;
    auto multiples = std::remove_if(vec.begin(), vec.end(), [k](auto&amp;&amp; i) { return i%k == 0; }); 
    if (multiples != vec.end()) ++res;
    auto half_multiples = std::remove_if(vec.begin(), multiples, [k](auto&amp;&amp; i) { return 2*(i%k) == k; });
    if (half_multiples != multiples) ++res;
    // 2. for each possible modulo-complementary couple, choose the one with the most occurrences
    std::transform(vec.begin(), half_multiples, vec.begin(), [k](auto&amp;&amp; i) {return i%k;}); // calculate modulos
    std::sort(vec.begin(), half_multiples);
    std::map&lt;int, int&gt; mods; // count their occurrences
    std::for_each(vec.begin(), half_multiples, [&amp;mods](auto&amp;&amp; i) {++mods[i];});
    // and then examine each possible couple
    auto uniques = std::unique(vec.begin(), half_multiples); 
    auto bottom_up = vec.begin();
    auto top_down = std::reverse_iterator&lt;std::vector&lt;int&gt;::iterator&gt;(uniques);
    while (bottom_up &lt; top_down.base()) {
        if (*bottom_up + *top_down == k) { // complementary
            res += std::max(mods[*bottom_up], mods[*top_down]);
            ++bottom_up, ++top_down;
        }
        // if not complementary advance the closest from its origin
        else if (*bottom_up &lt; k - *top_down ) { 
            res += mods[*bottom_up++];
        }
        else res += mods[*top_down++];
    }
    return res;
}


P.S: I've tried to submit that code and it passes correctly.