Grokking Oracle Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Count Number of Pairs With Absolute Difference K
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an array of integers nums and a positive integer k, return the number of pairs (i, j) where i < j and the absolute difference of nums[i] and nums[j] should be equal to k. In other words, |nums[i] - nums[j]| == k.

Examples

  • Example 1:

    • Input: nums = [1, 3, 5, 7], K = 2
    • Expected Output: 3
    • Justification: The pairs with an absolute difference of 2 are (1, 3), (3, 5), and (5, 7).
  • Example 2:

    • Input: nums = [4, 4, 4, 4], K = 1
    • Expected Output: 0
    • Justification: Each pair of the same elements has an absolute difference of 0. So, there are no pairs that have absolute difference 1.
  • Example 3:

    • Input: nums = [1,3,4,5,2], K = 2
    • Expected Output: 3
    • Justification: The pairs that satisfy the condition are (1, 3), (3, 5), and (4, 2).

Solution

To efficiently solve the problem, we can utilize a HashMap. This approach works by storing the occurrences of each number in the hashmap as we iterate through the array. By doing this, we can check in constant time whether there exists a number in the map such that the absolute difference with the current number is K. Specifically, for each number in the array, we check if either number + K or number - K exists in the HashMap.

This method is effective because it reduces the need for nested loops, thereby decreasing the overall time complexity to linear time, O(n), which is a significant improvement over the brute force approach. Additionally, it uses linear space to maintain the HashMap, making it a balanced approach in terms of both time and space efficiency.

Step-by-step Algorithm

  1. Initialize a HashMap to store each number's occurrence in the array.
  2. Initialize a count to 0 to keep track of the number of valid pairs.
  3. Iterate through the array:
    • For each number, check if (number + K) or (number - K) exists in the HashMap. If it does, increment the counter.
    • Update the HashMap with the current number's occurrence.
  4. Return the counter as the total number of valid pairs.

Algorithm Walkthrough

Let's consider the input nums = [1,3,4,5,2] with K = 2.

  • Initial State:

    • Hashmap: {}
    • nums = [1,3,4,5,2], K = 2
  • Step 1: Process nums[0] = 1

    • Calculate nums[i] + k = 1 + 2 = 3
    • Calculate nums[i] - k = 1 - 2 = -1
    • No matches in hashmap, so no pairs yet.
    • Update Hashmap with 1: 1 (number: frequency)
    • Hashmap: {1: 1}
  • Step 2: Process nums[1] = 3

    • Calculate nums[i] + k = 3 + 2 = 5
    • Calculate nums[i] - k = 3 - 2 = 1
    • Match found for 1 in hashmap, increment count by 1.
    • Update Hashmap with 3: 1
    • Hashmap: {1: 1, 3: 1}
    • Count = 1
  • Step 3: Process nums[2] = 4

    • Calculate nums[i] + k = 4 + 2 = 6
    • Calculate nums[i] - k = 4 - 2 = 2
    • No matches in hashmap, so count remains the same.
    • Update Hashmap with 4: 1
    • Hashmap: {1: 1, 3: 1, 4: 1}
  • Step 4: Process nums[3] = 5

    • Calculate nums[i] + k = 5 + 2 = 7
    • Calculate nums[i] - k = 5 - 2 = 3
    • Match found for 3 in hashmap, increment count by 1.
    • Update Hashmap with 5: 1
    • Hashmap: {1: 1, 3: 1, 4: 1, 5: 1}
    • Count = 2
  • Step 5: Process nums[4] = 2

    • Calculate nums[i] + k = 2 + 2 = 4
    • Calculate nums[i] - k = 2 - 2 = 0
    • Match found for 4 in hashmap, increment count by 1.
    • Update Hashmap with 2: 1
    • Hashmap: {1: 1, 3: 1, 4: 1, 5: 1, 2: 1}
    • Count = 3

Final Output

  • The final count of pairs with an absolute difference of K = 2 is 3.
Image

Code

Python3
Python3

. . . .

Complexity Analysis

  • Time Complexity: O(n), where n is the number of elements in the array. This is because we are iterating through the array exactly once, and for each element, we perform constant-time operations (insertion and lookup) in the HashMap.
  • *Space Complexity: O(n), as we are using a HashMap to store the occurrences of each element in the array, which in the worst case might contain all unique elements, requiring space proportional to the size of the array.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible