Pairs with difference k

QUESTION

Given an array of N positive integers a1, a2 ….. an. Find the number of pairs of integers whose difference is equal to a given number K.\nInput: The first line of the input contains ‘T’ denoting the total number of testcases.Then follows the description of testcases.\nThe first line of each testcase contains two space separated positive integers N and K denoting the size of array and the value of K. The second line contains N space separated positive integers denoting the elements of array.\nOutput: Output the number of pairs having difference equal to K in a separate line.\nConstraints:\n1<=T<=100\n1<=N<=100\n1<=K<=100\n1<=Arr[i]<=1000.

“TESTCASE_1”: “2\n5 2\n6 4 5 8 7\n4 2\n1 3 1 8\n###—###SEPERATOR—###—\n3\n2”, “TESTCASE_2”: “2\n5 2\n2 4 6 8 10\n4 2\n1 2 3 4\n###—###SEPERATOR—###—\n4\n2”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

``````import java.io.*;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int test = 0 ; test<t ; test++)
{
int count = 0;
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for(int i = 0 ; i<n ; i++)
arr[i] = sc.nextInt();
for(int i = 0 ; i<n-1 ; i++)
{
for(int j = i+1 ; j<n ; j++)
{
if(((arr[i] - k) == arr[j]) || ((arr[i] + k) == arr[j] ))
count++;
}
}
System.out.println(count);
}
}
}``````

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.