diff --git a/README.md b/README.md
index d39c9b4..99ba6a1 100644
--- a/README.md
+++ b/README.md
@@ -27,6 +27,20 @@
Solution |
Topics |
+
+ September 13th |
+ 1814. Count Nice Pairs in an Array |
+ $\text{\color{Dandelion}Medium}$ |
+
+ Counting & Hash Table
+ |
+
+ Array,
+ Counting,
+ Hash Table,
+ Math
+ |
+
September 12th |
2006. Count Number of Pairs With Absolute Difference K |
@@ -75,17 +89,6 @@
Simulation
-
- September 8th |
- 1155. Number of Dice Rolls With Target Sum |
- $\text{\color{Dandelion}Medium}$ |
-
- Dynamic Programming - Memoization
- |
-
- Dynamic Programming
- |
-
@@ -1451,6 +1454,21 @@
|
+
+ 1814 |
+ Count Nice Pairs in an Array |
+
+ Java
+ |
+ $\text{\color{Dandelion}Medium}$ |
+
+ Array,
+ Counting,
+ Hash Table,
+ Math
+ |
+ |
+
1833 |
Maximum Ice Cream Bars |
@@ -3377,6 +3395,21 @@
|
+
+ 1814 |
+ Count Nice Pairs in an Array |
+
+ Java
+ |
+ $\text{\color{Dandelion}Medium}$ |
+
+ Array,
+ Counting,
+ Hash Table,
+ Math
+ |
+ |
+
2006 |
Count Number of Pairs With Absolute Difference K |
@@ -5515,6 +5548,21 @@
|
+
+ 1814 |
+ Count Nice Pairs in an Array |
+
+ Java
+ |
+ $\text{\color{Dandelion}Medium}$ |
+
+ Array,
+ Counting,
+ Hash Table,
+ Math
+ |
+ |
+
1832 |
Check if the Sentence is Pangram |
@@ -6459,6 +6507,21 @@
Bit Manipulation
+
+ 1814 |
+ Count Nice Pairs in an Array |
+
+ Java
+ |
+ $\text{\color{Dandelion}Medium}$ |
+
+ Array,
+ Counting,
+ Hash Table,
+ Math
+ |
+ |
+
1979 |
Find Greatest Common Divisor of Array |
diff --git a/solutions/1814. Count Nice Pairs in an Array/CountNicePairsInAnArray.java b/solutions/1814. Count Nice Pairs in an Array/CountNicePairsInAnArray.java
new file mode 100644
index 0000000..18f58cc
--- /dev/null
+++ b/solutions/1814. Count Nice Pairs in an Array/CountNicePairsInAnArray.java
@@ -0,0 +1,50 @@
+package com.cheehwatang.leetcode;
+
+import java.util.HashMap;
+import java.util.Map;
+
+// Time Complexity : O(n),
+// where 'n' is the length of 'nums'.
+// We traverse 'nums' to determine and count the relative values.
+//
+// Space Complexity : O(n),
+// where 'n' is the length of 'nums'.
+// We use a Hash Table to count the relative values, with a maximum size of 'n'.
+
+public class CountNicePairsInAnArray {
+
+ // Approach:
+ // First we need to understand the bad pair, i < j and nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]).
+ // With the brute force, we can check each and every pair if it is a nice pair,
+ // but the algorithm will exceed the time limit.
+ // Thus, let's change the equation for the bad pair as follows:
+ // nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]).
+ // Use a HashMap to record the nums[i] - rev(nums[i]) for all the numbers, and the frequency.
+
+ public int countNicePairs(int[] nums) {
+
+ // Use long type, so as not the keep doing the modulo function, only need to do once at the end.
+ long nicePairs = 0;
+ Map map = new HashMap<>();
+
+ for (int number : nums) {
+ // Calculate the difference (relative value) between the nums[i] and rev(nums[i]).
+ int relative = number - reverse(number);
+ // If the map already had numbers with the same relative value, then add the frequency as the nice pairs.
+ nicePairs += map.getOrDefault(relative, 0);
+ // Increase the count in the map.
+ map.put(relative, map.getOrDefault(relative, 0) + 1);
+ }
+ return (int) (nicePairs % (1e9 + 7));
+ }
+
+ // Method to reverse the number.
+ private int reverse(int number) {
+ int reverse = 0;
+ while (number > 0) {
+ reverse = reverse * 10 + number % 10;
+ number /= 10;
+ }
+ return reverse;
+ }
+}