Advertisement
nikunjsoni

1814

Apr 4th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. class Solution {
  2.     int rev(int x) {
  3.         string s = to_string(x);
  4.         reverse(s.begin(), s.end());
  5.         x = stoi(s);
  6.         return x;
  7.     }
  8. public:
  9.     int countNicePairs(vector<int>& nums) {
  10.         unordered_map<int, int> ctr;
  11.         for(int x : nums)
  12.             ++ctr[x - rev(x)];
  13.         const int mod = (int)1e9 + 7;
  14.         int ans = 0;
  15.         for(auto it : ctr)
  16.             ans = (ans + it.second * (it.second - 1LL) / 2) % mod;
  17.         return ans;
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement