• + 11 comments

    Thank you for the insight! Since many others are posting code for this problem, I'd like to suggest that c++ isn't too far from python this time :-)

    int n, d;
    cin >> n >> d;
    
    vector<int> vals(n);
    for (auto& val : vals)
        cin >> val;
    
    auto exists = [&](int val)
    { return binary_search(begin(vals), end(vals), val); };
    
    auto result = count_if(
        begin(vals), end(vals),
        [&](int val) {
            return exists(val + d) && exists(val + 2 * d);
        });
    
    cout << result << endl;
    

    `