1814. Count Nice Pairs in an Array LeetCode Solution

In this guide, you will get 1814. Count Nice Pairs in an Array LeetCode Solution with the best time and space complexity. The solution to Count Nice Pairs in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Nice Pairs in an Array solution in C++
  4. Count Nice Pairs in an Array solution in Java
  5. Count Nice Pairs in an Array solution in Python
  6. Additional Resources
1814. Count Nice Pairs in an Array LeetCode Solution image

Problem Statement of Count Nice Pairs in an Array

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 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 <= 105
0 <= nums[i] <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1814. Count Nice Pairs in an Array LeetCode Solution in C++

class Solution {
 public:
  int countNicePairs(vector<int>& nums) {
    constexpr int kMod = 1'000'000'007;
    long ans = 0;
    unordered_map<int, long> count;

    for (const int num : nums)
      ++count[num - rev(num)];

    for (const auto& [_, freq] : count) {
      ans += freq * (freq - 1) / 2;
      ans %= kMod;
    }

    return ans;
  }

 private:
  int rev(int n) {
    int x = 0;
    while (n > 0) {
      x = x * 10 + (n % 10);
      n /= 10;
    }
    return x;
  }
};
/* code provided by PROGIEZ */

1814. Count Nice Pairs in an Array LeetCode Solution in Java

class Solution {
  public int countNicePairs(int[] nums) {
    final int kMod = 1_000_000_007;
    long ans = 0;
    Map<Integer, Long> count = new HashMap<>();

    for (final int num : nums)
      count.merge(num - rev(num), 1L, Long::sum);

    for (final long freq : count.values()) {
      ans += freq * (freq - 1) / 2;
      ans %= kMod;
    }

    return (int) ans;
  }

  private int rev(int n) {
    int x = 0;
    while (n > 0) {
      x = x * 10 + (n % 10);
      n /= 10;
    }
    return x;
  }
}
// code provided by PROGIEZ

1814. Count Nice Pairs in an Array LeetCode Solution in Python

class Solution:
  def countNicePairs(self, nums: list[int]) -> int:
    freqs = collections.Counter(num - int(str(num)[::-1]) for num in nums)
    return sum(freq * (freq - 1) // 2 for freq in freqs.values()) % 1000000007
# code by PROGIEZ

Additional Resources

See also  2488. Count Subarrays With Median K LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.