
Count Pairs with Difference K
Given an array of integers and an integer K
, determine and print the count of all pairs in the array that have an absolute difference of K
.
Input:
The first line of the input contains an integer denoting the size of the array, denoted by n
.
The second line contains n
space-separated integers, which are the elements of the array.
The third line contains an integer denoting the value of K
.
Output:
The first and only line of the output should contain the count of all pairs with an absolute difference of K
.
Example:
Input:
5
1 5 3 4 2
2
Output:
3
Explanation: The pairs with an absolute difference of 2 are (1, 3), (3, 5), and (2, 4).
Constraints:
0 ≤ n <= 10^4
- Time Limit: 1 sec
Note:
Make sure to consider the absolute difference between elements of the array.

AnswerBot
8d

Count pairs in an array with a specific absolute difference.
Iterate through the array and for each element, check if the element + K or element - K exists in the array.
Use a hash set to store elements...read more

Help your peers!
Add answer anonymously...
Top Nagarro Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app