with Java please K-Difference Complete the kDifference function in the editor be
ID: 3846807 • Letter: W
Question
with Java please
K-Difference Complete the kDifference function in the editor below. It has two parameters: 1. An array of n positive distinct integers, a. 2. An integer, k. The function must return an integer denoting the total number of pairs of elements, (ai in array a where ai k a Input Format Locked stub code in the editor reads the following input from stdin and passes it to the function: The first line contains an integer, n, denoting the number of elements in array a. Each line i of the n subsequent lines (where 0 si k n) contains an integer describing a The last line contains an integer, k. Constraints 5 s n s 105 Each ai s 2 x 109 1 s k s 109 Output Format The function must return an integer denoting the number of pairs in array a whose difference is k. This is printed to stdout by locked stub code in the editor. Sample input 0 Sample output 0Explanation / Answer
static int KDifference(int arr[], int k)
{
int count = 0;
for (int i = 0; i < a.length; i++)
{
for (int j = i+1; j < a.length; j++)
if (arr[i] - arr[j] == k || arr[j] - arr[i] == k )
count++;
}
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.