Watch 10 video solutions for Count Nice Pairs in an Array, a medium level problem involving Array, Hash Table, Math. This walkthrough by codestorywithMIK has 6,824 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an array nums that consists of non-negative integers. Let us define rev(x) as the reverse of the non-negative integer x. For example, rev(123) = 321, and rev(120) = 21. A pair of indices (i, j) is nice if it satisfies all of the following conditions:
0 <= i < j < nums.lengthnums[i] + rev(nums[j]) == nums[j] + rev(nums[i])Return the number of nice pairs of indices. Since that number can be too large, return it modulo 109 + 7.
Example 1:
Input: nums = [42,11,1,97] Output: 2 Explanation: The two pairs are: - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121. - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.
Example 2:
Input: nums = [13,10,35,24,76] Output: 4
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 109Problem Overview: You are given an integer array nums. A pair (i, j) is considered nice if nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]), where rev(x) reverses the digits of x. The goal is to count how many such pairs exist with i < j, returning the result modulo 1e9 + 7.
Approach 1: Two-Pointer / Pairwise Check (Brute Force) (O(n²) time, O(1) space)
The most direct approach checks every pair of indices. Use two nested loops: the outer loop selects index i, and the inner loop iterates through j > i. For each pair, compute rev(nums[i]) and rev(nums[j]), then verify whether the equation nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]) holds. This method requires no extra data structures and only constant additional memory. The drawback is the quadratic runtime, which becomes too slow when the array size grows to tens of thousands. Still, it clearly demonstrates the pair relationship and is useful as a baseline solution during interviews.
Approach 2: Difference with Reverse (Hash Map Counting) (O(n) time, O(n) space)
The key observation simplifies the equation. Rearranging nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]) gives nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]). Instead of comparing pairs directly, compute a transformed value diff = num - rev(num) for each element. Two indices form a nice pair if they share the same diff. Use a hash map to track how many times each difference has appeared. While iterating through the array, look up the current diff in the map and add its frequency to the answer because each previous occurrence forms a new pair. Then increment the stored count.
This technique converts a pair comparison problem into a frequency counting problem using a hash table. Each element is processed once, giving O(n) time complexity. The extra space stores counts of differences, which in the worst case grows to O(n). The digit reversal operation is constant time because the number of digits in an integer is bounded.
Conceptually, the algorithm combines ideas from array traversal and frequency counting. Once the algebraic transformation is recognized, the implementation becomes short and efficient.
Recommended for interviews: Start by describing the brute-force pairwise comparison to show you understand the definition of a nice pair. Then derive the transformation num - rev(num) and switch to the hash map counting approach. Interviewers expect the O(n) solution because it demonstrates pattern recognition and the ability to convert pair constraints into a frequency grouping problem.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-Pointer / Pairwise Check (Brute Force) | O(n²) | O(1) | Small arrays or when demonstrating the direct definition of a nice pair |
| Difference with Reverse (Hash Map Counting) | O(n) | O(n) | General case and interview settings where linear performance is required |