Why N*N solution giving TLE explained ,How to get rid of TLE with n*n Solution.
Intuition\nHere We have to reduce the number of Operations , as much as Possible.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nH
# Intuition\nHere We have to reduce the number of Operations , as much as Possible.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nHere the N=5000 , Therefore if your solution is having N*N , you are considering to pass the Testcase , but the thing to be noticed is that, N*N = 2.5 * 1e7, atmost 1e8 operations can be performed , so we have truely consicous ,while performing the operation in innermost loop. \nBecause if around 40-50 operations are performed that means that around 5*1e8, operation are performed. Which means it will req 5 sec indeed giving TLE,\n\nHere is my code , which doesn\'t req that much explantion , because the approach almost everyone has understood. The Major part is how to apply In Most Optimised Way.\n\n\nThank you. Please Upvote if you found it Useful. \n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N*N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums) {\n        int cnt = 0;\n        int n = nums.size();\n        \n        for(int i = 0; i < n; i++) {\n            for(int j = i + 1; j < n; j++) {\n                int v1 = nums[i];\n                int v2 = nums[j];\n                \n                if (v1 == v2) {\n                    cnt++;\n                    continue;\n                }\n                \n                int digits1[4], digits2[4];\n                int idx = 0, mismatch = 0;\n                \n                while (v1 != 0 || v2 != 0) {\n                    int d1 = v1 % 10;\n                    int d2 = v2 % 10;\n                    \n                    if (d1 != d2) {\n                        if (mismatch < 4) {\n                            digits1[mismatch] = d1;\n                            digits2[mismatch] = d2;\n                        }\n                        mismatch++;\n                        if (mismatch > 4) break;\n                    }\n                    \n                    v1 /= 10;\n                    v2 /= 10;\n                }\n                \n                if (mismatch == 2) {\n                    if (digits1[0] == digits2[1] && digits1[1] == digits2[0]) {\n                        cnt++;\n                    }\n                } else if (mismatch == 3) {\n                    if ((digits1[0] == digits2[1] && digits2[0] == digits1[2] && digits1[1] == digits2[2]) ||\n                        (digits1[1] == digits2[0] && digits1[0] == digits2[2] && digits1[2] == digits2[1])) {\n                        cnt++;\n                    }\n                } else if (mismatch == 4) {\n                    if ((digits1[0] == digits2[1] && digits1[1] == digits2[0] && \n                         digits1[2] == digits2[3] && digits1[3] == digits2[2]) || \n                        (digits1[0] == digits2[2] && digits1[2] == digits2[0] &&\n                         digits1[1] == digits2[3] && digits1[3] == digits2[1]) ||\n                         (digits1[0] == digits2[3] && digits1[3] == digits2[0] &&\n                         digits1[1] == digits2[2] && digits1[2] == digits2[1])\n                        ) {\n                        cnt++;\n                    }\n                }\n            }\n        }\n        return cnt;\n    }\n};\n\n```
['C++']