Find the number of element whose difference is K

#include <iostream>
#include <vector>
#include <algorithm>

int solve ( int *input, int N, int K) {
    int count = 0;
    std::sort ( input, input + N);
    for ( int i = 0; i < N; i++) {
        int find_me  = input[i] + K;
        if (std::binary_search (input + i + 1, input + N, find_me )) 
            count++;        
    }
    return count;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s