Answers for "given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k."

C++
2

two elements with difference K in c++

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;
}
Posted by: Guest on July-01-2020

Code answers related to "given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k."

Browse Popular Code Answers by Language