<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it satisfies all of the following conditions:</p>

<ul>
	<li><code>0 &lt;= i &lt; j &lt; nums.length</code></li>
	<li><code>nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])</code></li>
</ul>

<p>Return <em>the number of nice pairs of indices</em>. Since that number can be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [42,11,1,97]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [13,10,35,24,76]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int factorial(int n){
        if(n == 0 || n == 1) return 1;
        int fact = 1;
        for(int i = 2;i<=n;i++){
            fact *= i;
        }
        return fact;
    }
    int combination(int freq, int n){
        return factorial(freq)/(factorial(n)*factorial(freq-n));
    }
    int Rev(int a){
        int r = 0;
        while(a>0){
            r *= 10;
            r+=(a%10);
            a /= 10;
        }
        return r;
    }
    int countNicePairs(vector<int>& nums) {
        int n = nums.size();
        
        //cpoy them into a reverse array
        vector<int> rev(n);
        for(int i=0;i<n;i++){
            rev[i]  = Rev(nums[i]);
        }

        //nums[i] - rev(nums[i]) == nums[j] - rev(nums[j])
        vector<int> diff(n);
        for(int i=0;i<n;i++){
            diff[i] = nums[i] - rev[i];
        }

        // store in a hash map with freq
        unordered_map<int,int> m;
        for(int i=0;i<n;i++){
            m[diff[i]]++;
        }

        int pairs = 0;
        const int MOD = 1e9 + 7; 
        for(auto p : m){
            int freq = p.second;
            if(freq > 1) pairs = (pairs + (long long)freq * (freq - 1) / 2) % MOD;
        }

        return pairs;
    }
};
65000