Count Nice Pairs in an Array - Complete Solution Guide
Count Nice Pairs in an Array is LeetCode problem 1814, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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.length nums[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 10 9 + 7 . Example 1: Input: nums = [42,11,1,97] Output: 2 E
Detailed Explanation
The problem asks us to find the number of 'nice pairs' in a given array of non-negative integers. A pair of indices (i, j) is considered 'nice' if i < j and nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]), where rev(x) is the reverse of the integer x. The final answer should be returned modulo 10^9 + 7.
Solution Approach
The solution calculates the difference between each number in the input array and its reverse. It then uses a hash table to store the frequency of each difference encountered. For each number, it checks how many times its calculated difference has already been seen. This count is added to the total number of nice pairs. Finally, the total count is returned modulo 10^9 + 7.
Step-by-Step Algorithm
- Step 1: Initialize a hash table `freq` to store the frequency of the differences between numbers and their reverses. Also initialize a variable `count` to 0 to keep track of the number of nice pairs.
- Step 2: Iterate through the input array `nums`.
- Step 3: For each number `num` in `nums`, calculate its reverse `rev_num`.
- Step 4: Calculate the difference `diff = num - rev_num`.
- Step 5: Check if `diff` exists as a key in the `freq` hash table. If it does, increment `count` by the value associated with `diff` (which is the number of times that difference has appeared so far). Remember to take the modulo 10^9 + 7 of the `count` after incrementing.
- Step 6: Increment the frequency of `diff` in the `freq` hash table.
- Step 7: After iterating through all the numbers in `nums`, return `count`.
Key Insights
- Insight 1: The core condition `nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])` can be simplified to `nums[i] - rev(nums[i]) == nums[j] - rev(nums[j])`. This means we only need to calculate this difference for each number and count how many numbers have the same difference.
- Insight 2: Using a hash table (or dictionary) to store the frequency of each difference is crucial for efficiently counting the pairs. This allows us to achieve a linear time complexity.
- Insight 3: The modulo operation is necessary to prevent integer overflow, as the number of nice pairs can be quite large.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Math, Counting.
Companies
Asked at: Block.