Skip to content

Commit f17a3a9

Browse files
authored
Merge pull request #331 from cheehwatang/add-2006-CountNumberOfPairsWithAbsoluteDifferenceK
Add 2006. Count Number of Pairs With Absolute Difference K
2 parents bef415f + 92785bd commit f17a3a9

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 10th</td>
32+
<td>2006. <a href="https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/">Count Number of Pairs With Absolute Difference K</a></td>
33+
<td align="center">$\text{\color{TealBlue}Easy}$</td>
34+
<td align="center">
35+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/2006.%20Count%20Number%20of%20Pairs%20With%20Absolute%20Difference%20K/CountNumberOfPairsWithAbsoluteDifferenceK.java">Brute Force</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>
39+
</td>
40+
</tr>
3041
<tr>
3142
<td align="center">September 9th</td>
3243
<td>2028. <a href="https://leetcode.com/problems/find-missing-observations/">Find Missing Observations</a></td>
@@ -75,18 +86,6 @@
7586
<a href="#memoization">Memoization</a>
7687
</td>
7788
</tr>
78-
<tr>
79-
<td align="center">September 5th</td>
80-
<td>1137. <a href="https://leetcode.com/problems/n-th-tribonacci-number/">N-th Tribonacci Number</a></td>
81-
<td align="center">$\text{\color{TealBlue}Easy}$</td>
82-
<td align="center">
83-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1137.%20N-th%20Tribonacci%20Number/NthTribonacciNumber_Iterative.java">Dynamic Programming</a>
84-
</td>
85-
<td align="center">
86-
<a href="#dynamic-programming">Dynamic Programming</a>,
87-
<a href="#math">Math</a>
88-
</td>
89-
</tr>
9089
</table>
9190
</br>
9291
<hr>
@@ -1563,6 +1562,18 @@
15631562
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/1995.%20Count%20Special%20Quadruplets/CountSpecialQuadruplets_HashTable.java"><em>Hash Table</em></a>
15641563
</td>
15651564
</tr>
1565+
<tr>
1566+
<td align="center">2006</td>
1567+
<td><a href="https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/">Count Number of Pairs With Absolute Difference K</a></td>
1568+
<td align="center">
1569+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/2006.%20Count%20Number%20of%20Pairs%20With%20Absolute%20Difference%20K/CountNumberOfPairsWithAbsoluteDifferenceK.java">Java</a>
1570+
</td>
1571+
<td align="center">$\text{\color{TealBlue}Easy}$</td>
1572+
<td align="center">
1573+
<a href="#array">Array</a>
1574+
</td>
1575+
<td></td>
1576+
</tr>
15661577
<tr>
15671578
<td align="center">2007</td>
15681579
<td><a href="https://leetcode.com/problems/find-original-array-from-doubled-array/">Find Original Array From Doubled Array</a></td>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cheehwatang.leetcode;
2+
3+
// Time Complexity : O(n^2),
4+
// where 'n' is the length of 'nums'.
5+
// We traverse 'nums' in a nested for-loop to count the pair with 'k' differences.
6+
//
7+
// Space Complexity : O(1),
8+
// as the auxiliary space used is independent of the input.
9+
10+
public class CountNumberOfPairsWithAbsoluteDifferenceK {
11+
12+
// Approach:
13+
// Intuitive and brute force approach to check every combination and count the ones with 'k' difference.
14+
15+
public int countKDifference(int[] nums, int k) {
16+
int count = 0;
17+
for (int i = 0; i < nums.length - 1; i++) {
18+
for (int j = i + 1; j < nums.length; j++) {
19+
if (Math.abs(nums[i] - nums[j]) == k) count++;
20+
}
21+
}
22+
return count;
23+
}
24+
}

0 commit comments

Comments
 (0)