java.lang.Object
g2001_2100.s2006_count_number_of_pairs_with_absolute_difference_k.Solution

public class Solution extends Object
2006 - Count Number of Pairs With Absolute Difference K.<p>Easy</p> <p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of pairs</em> <code>(i, j)</code> <em>where</em> <code>i < j</code> <em>such that</em> <code>|nums[i] - nums[j]| == k</code>.</p> <p>The value of <code>|x|</code> is defined as:</p> <ul> <li><code>x</code> if <code>x >= 0</code>.</li> <li><code>-x</code> if <code>x < 0</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,2,1], k = 1</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The pairs with an absolute difference of 1 are:</p> <ul> <li> <p>[<strong>1</strong> , <strong>2</strong> ,2,1]</p> </li> <li> <p>[<strong>1</strong> ,2, <strong>2</strong> ,1]</p> </li> <li> <p>[1, <strong>2</strong> ,2, <strong>1</strong> ]</p> </li> <li> <p>[1,2, <strong>2</strong> , <strong>1</strong> ]</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,3], k = 3</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There are no pairs with an absolute difference of 3.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [3,2,1,5,4], k = 2</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The pairs with an absolute difference of 2 are:</p> <ul> <li> <p>[<strong>3</strong> ,2, <strong>1</strong> ,5,4]</p> </li> <li> <p>[<strong>3</strong> ,2,1, <strong>5</strong> ,4]</p> </li> <li> <p>[3, <strong>2</strong> ,1,5, <strong>4</strong> ]</p> </li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 200</code></li> <li><code>1 <= nums[i] <= 100</code></li> <li><code>1 <= k <= 99</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countKDifference

      public int countKDifference(int[] nums, int k)