Count Number of Pairs With Absolute Difference K

🏠 ⬅️ ➡️

Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.

The value of |x| is defined as:

  • x if x >= 0.
  • -x if x < 0.

Example 1:

Input: nums = [1,2,2,1], k = 1 Output: 4 Explanation: The pairs with an absolute difference of 1 are: - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] - [1,2,2,1]

Example 2:

Input: nums = [1,3], k = 3 Output: 0 Explanation: There are no pairs with an absolute difference of 3.

Example 3:

Input: nums = [3,2,1,5,4], k = 2 Output: 3 Explanation: The pairs with an absolute difference of 2 are: - [3,2,1,5,4] - [3,2,1,5,4] - [3,2,1,5,4]

Constraints:

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
  • 1 <= k <= 99

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solve the problem described in the task
    implicit none

    integer, parameter :: n = 5
    integer, dimension(n) :: nums = (/ 1, 2, 2, 1, 3 /)
    integer, parameter :: k = 1
    integer :: i, j
    integer :: count

    count = 0
    do i = 1, n - 1
        do j = i + 1, n
            if (abs(nums(i) - nums(j)) == k) then
                count = count + 1
            end if
        end do
    end do

    print '(I0)', count

end program main
Compiled
Executed
⚠️ Correct
module pairwise_difference
    implicit none
    private
    public :: pairwise_difference_solution

contains

    function pairwise_difference_solution(nums, k) result(count)
        integer, intent(in) :: nums(:)
        integer, intent(in) :: k
        integer :: count
        integer :: i, j

        count = 0
        do i = 1, size(nums) - 1
            do j = i + 1, size(nums)
                if (abs(nums(i) - nums(j)) == k) then
                    count = count + 1
                end if
            end do
        end do
    end function pairwise_difference_solution
end module pairwise_difference

program pairwise_difference_main
    use pairwise_difference
    implicit none
    integer, parameter :: nums1(4) = [1, 2, 2, 1]
    integer, parameter :: nums2(2) = [1, 3]
    integer, parameter :: nums3(5) = [3, 2, 1, 5, 4]
    integer, parameter :: k1 = 1
    integer, parameter :: k2 = 3
    integer, parameter :: k3 = 2
    integer :: count1, count2, count3

    count1 = pairwise_difference_solution(nums1, k1)
    count2 = pairwise_difference_solution(nums2, k2)
    count3 = pairwise_difference_solution(nums3, k3)

    write (*,*) "nums1:", nums1
    write (*,*) "k1:", k1
    write (*,*) "count1:", count1
    write (*,*) "nums2:", nums2
    write (*,*) "k2:", k2
    write (*,*) "count2:", count2
    write (*,*) "nums3:", nums3
    write (*,*) "k3:", k3
    write (*,*) "count3:", count3
end program pairwise_difference_main
🌐 Data from online sources
def chalk_replacer(chalk, k):
    total_chalk = sum(chalk)
    k %= total_chalk
    for i, val in enumerate(chalk):
        if k < val:
            return i
        k -= val
    return 0
  1. Calculate the sum of all elements in the chalk array, representing the total chalk needed for one full cycle.
  2. Calculate the remainder of k divided by the total chalk needed using modulo operation (k %= sum). This will determine the remaining chalk when the teacher starts the last cycle.
  3. Iterate through the chalk array:
  4. If the remaining chalk (k) is less than the chalk needed by the current student (chalk[i] for C++ and Java, or val for Python and JavaScript), return the index of this student.
  5. Otherwise, update the remaining chalk by subtracting the chalk needed by the current student from k.

Repeat this process until a student is found who will replace the chalk.

🌐 Data from online sources
#include <vector>

int chalkReplacer(std::vector<int>& chalk, int k) {
    long long sum = 0;
    for (int i = 0; i < chalk.size(); i++) {
        sum += chalk[i];
    }
    k %= sum;
    for (int i = 0; i < chalk.size(); i++) {
        if (k < chalk[i]) {
            return i;
        }
        k -= chalk[i];
    }
    return 0;
}
  1. Calculate the sum of all elements in the chalk array, representing the total chalk needed for one full cycle.
  2. Calculate the remainder of k divided by the total chalk needed using modulo operation (k %= sum). This will determine the remaining chalk when the teacher starts the last cycle.
  3. Iterate through the chalk array:
  4. If the remaining chalk (k) is less than the chalk needed by the current student (chalk[i] for C++ and Java, or val for Python and JavaScript), return the index of this student.
  5. Otherwise, update the remaining chalk by subtracting the chalk needed by the current student from k.

Repeat this process until a student is found who will replace the chalk.