Video Explanation (Optimising brute force solution using pre-computation)
Explanation\n\nClick here for the video\n\n# Code\ncpp []\nconst int N = 1e7;\nint cnt[N];\n\nclass Solution {\n    \n    inline int MakeNumber (vector<int>& di
# Explanation\n\n[Click here for the video](https://youtu.be/t_1vwZbUmzI)\n\n# Code\n```cpp []\nconst int N = 1e7;\nint cnt[N];\n\nclass Solution {\n    \n    inline int MakeNumber (vector<int>& digits) {\n        int ans = 0;\n        for (auto it = digits.rbegin(); it != digits.rend(); it ++)\n            ans = (ans * 10) + *it;\n        return ans;\n    } \n    \npublic:\n    int countPairs(vector<int>& nums) {\n        memset(cnt, 0, sizeof(cnt));\n        int ans = 0;\n        int n = 7;\n        vector<int> digits(7, 0);\n        \n        for (auto i : nums) {\n            ans += cnt[i];\n            \n            int ind = 0;\n            while (i) {\n                digits[ind ++] = i%10;\n                i /= 10;\n            }\n            while (ind < 7) digits[ind++] = 0;\n            \n            unordered_set<int> all;\n            for (int l1 = 0; l1 < n; l1 ++) {\n                for (int r1 = l1+1; r1 < n; r1 ++) {\n                    swap (digits[l1], digits[r1]);                            \n                    all.insert(MakeNumber(digits));\n                    swap (digits[l1], digits[r1]);\n                    \n                    for (int l2 = 0; l2 < n; l2 ++) {\n                        for (int r2 = l2+1; r2 < n; r2 ++) {\n                            swap (digits[l1], digits[r1]);\n                            swap (digits[l2], digits[r2]);\n                            \n                            all.insert(MakeNumber(digits));\n                            \n                            swap (digits[l2], digits[r2]);\n                            swap (digits[l1], digits[r1]);\n                        }\n                    }\n                }\n            } \n            for (auto it : all) cnt[it] ++;\n        }\n        return ans;\n    }\n};\n```
['C++']