Java Passes beats 100% but same code TLE in C++
Complexity\n- Time complexity: O(n^2 * 7) \n\n- Space complexity: O(1) \n\nC++ []\n// TLE 625/630 same logic passes in Java\nclass Solution {\npublic:\n    int 
# Complexity\n- Time complexity: $$O(n^2 * 7)$$ \n\n- Space complexity: $$O(1)$$ \n\n``` C++ []\n// TLE 625/630 same logic passes in Java\nclass Solution {\npublic:\n    int countPairs(vector<int>& a) {\n        int n=a.size(),ans=0;\n        for(int i=0;i<n;i++){\n            for(int j=i+1;j<n;j++)\n            {\n                int y=a[j],x=a[i],ok=0;\n                vector<int> dx,dy;\n                while(x + y){\n                    if(x%10!=y%10){dx.push_back(x%10);dy.push_back(y%10);ok++;}\n                    x/=10;y/=10;\n                }\n                if(ok==0)ans++;\n                if(ok==2){\n                    if(dx[0]==dy[1] && dx[1]==dy[0])ans++;\n                }\n                if(ok==3){\n                    sort(dx.begin(),dx.end());\n                    sort(dy.begin(),dy.end());\n                    if(dx==dy)ans++;\n                }\n                if(ok==4){\n                    if(dx[0]==dy[1] && dx[1]==dy[0] && dx[2]==dy[3] && dx[3]==dy[2])ans++;\n                    else if(dx[0]==dy[2] && dx[1]==dy[3] && dx[2]==dy[0] && dx[3]==dy[1])ans++;\n                    else if(dx[0]==dy[3] && dx[1]==dy[2] && dx[2]==dy[1] && dx[3]==dy[0])ans++;\n                }\n            }   \n        }\n        return ans;\n    }\n};\n```\n\n# Code\n```java []\nclass Solution {\n    public int countPairs(int[] a) {\n        int n = a.length, ans = 0;\n        for (int i = 0; i < n; i++) {\n            for (int j = i + 1; j < n; j++) {\n                int y = a[j], x = a[i], ok = 0;\n                int[] dx = new int[4], dy = new int[4];\n                int idx = 0;\n                \n                while (x + y > 0) {\n                    if (x % 10 != y % 10) {\n                        \n                    if(idx==4){\n                        idx=-1;\n                        break;\n                    }\n                        dx[idx] = x % 10;\n                        dy[idx] = y % 10;\n                        ok++;\n                        idx++;\n                    }\n                    x /= 10;\n                    y /= 10;\n                }\n                if(idx==-1)continue;\n\n                if (ok == 0) {\n                    ans++;\n                    continue;\n                }\n                \n                if (ok == 2) {\n                    if (dx[0] == dy[1] && dx[1] == dy[0]) ans++;\n                } else if (ok == 3) {\n                    Arrays.sort(dx, 0, 3);\n                    Arrays.sort(dy, 0, 3);\n                    if (Arrays.equals(dx, dy)) ans++;\n                } else if (ok == 4) {\n                    if (dx[0] == dy[1] && dx[1] == dy[0] && dx[2] == dy[3] && dx[3] == dy[2]) ans++;\n                    else if (dx[0] == dy[2] && dx[1] == dy[3] && dx[2] == dy[0] && dx[3] == dy[1]) ans++;\n                    else if (dx[0] == dy[3] && dx[1] == dy[2] && dx[2] == dy[1] && dx[3] == dy[0]) ans++;\n                }\n            }\n        }\n        return ans;\n    }\n}\n\n```
['Java']