Check out example codes for "two elements with difference K in c++". It will help you in understanding the concepts better.

Code Example 1

bool diffK(int A[], int N, int K) {
    sort(A, A+N);
    int i = 0, j = 0;
    while (i < N && j < N) {
        if (A[i] == A[j] + K) return true;
        else if (A[i] < A[j] + K) i++;
        else j++;
    }
    return false;
}

Learn ReactJs, React Native from akashmittal.com