Generate swaps for each and check.
Key takeaway is to sort the array in descending order because we can swap the digits of bigger number to get the smaller number but not vice versa.\n\nIn the ge
Key takeaway is to sort the array in descending order because we can swap the digits of bigger number to get the smaller number but not vice versa.\n\nIn the getswap function I have used an unordered_set because duplicate numbers can be made from 1 swap and 2 swaps. \n\nIf the current nums[i] value is present in the map that means some number/s on being swapped (or not being swapped) give nums[i], therefore add the value of m[nums[i]] to the total count.\n\n# Complexity\n- Time complexity: O(n * 32 * 32 + nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n * 32 * 32)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n    void getswap(int x, unordered_map <int, int> &m) {\n\n        string s = to_string(x);\n\n        int n = s.size();\n\n        unordered_set <int> st;\n\n\n        for(int i = 0; i < n; i++) {\n\n            for(int j = i + 1; j < n; j++) {\n\n                if(s[i] == s[j]) continue;\n\n                swap(s[i], s[j]);\n\n                // m[stoi(s)]++;\n\n                st.insert(stoi(s));\n\n                for(int k = 0; k < n; k++) {\n\n                    for(int l = k + 1; l < n; l++) {\n\n\n                        if((k == i and l == j) or s[k] == s[l]) continue;\n\n                        swap(s[k], s[l]);\n\n                        // m[stoi(s)]++;\n                        st.insert(stoi(s));\n\n                        swap(s[k], s[l]);\n\n                    }\n\n                }\n\n                swap(s[i], s[j]);\n\n                \n            }\n        }\n\n        for(auto i: st) {\n\n            m[i]++;\n        }\n\n\n    }\n\n\n    int countPairs(vector<int>& nums) {\n\n        unordered_map <int, int> m;\n\n        int cnt = 0;\n\n        sort(nums.begin(), nums.end(), greater <int>());\n\n        for(int i = 0; i < nums.size(); i++) {\n\n            if(m.find(nums[i]) != m.end()) {\n\n                cnt += m[nums[i]];\n            }\n\n            // else {\n\n                m[nums[i]]++;\n\n                getswap(nums[i], m);\n            // }\n\n\n        }\n\n        // for(auto i: m) {\n\n        //     cout << i.first << ": " << i.second << endl;\n        // }\n\n        return cnt;\n        \n    }\n};\n```
['Array', 'Hash Table', 'Sorting', 'C++']