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
4mo
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...
Nagarro Software Developer interview questions & answers
A Software Developer was asked 3mo agoQ. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 3mo agoQ. Given the root of a binary search tree, and an integer k, return the kth smalles...read more
A Software Developer was asked 5mo agoQ. What is abstraction in Java?
Popular interview questions of Software Developer
A Software Developer was asked 3mo agoQ1. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 3mo agoQ2. Given the root of a binary search tree, and an integer k, return the kth smalles...read more
A Software Developer was asked 5mo agoQ3. What is abstraction in Java?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

