Pure maths C++ || Java Solution -- beats 100% in memory & runtime
Intuition\n\n\nJava []\nclass Solution {\n    public long[] countOfPairs(int n, int x, int y) {\n        if (x > y) {\n            return countOfPairs(n, y, x);
# Intuition\n![Screenshot 2024-01-21 184846.png](https://assets.leetcode.com/users/images/b3578d16-40b5-4f72-9118-cb939cfe00e7_1705843256.1118536.png)\n![Screenshot 2024-01-21 185003.png](https://assets.leetcode.com/users/images/6542cb4e-8d77-462b-b4d8-39dae51e5444_1705843263.3069587.png)\n```Java []\nclass Solution {\n    public long[] countOfPairs(int n, int x, int y) {\n        if (x > y) {\n            return countOfPairs(n, y, x);\n        }\n\n        if (x + 1 == y) {\n            return countOfPairs(n, x, x);\n        }\n\n        int lenWithoutRing = n - (y - x) + 1;\n        int ring = y - x + 1;\n        long[] result = new long[n];\n        for (int k = 1; k < n; k++) {\n            if (x == y) {\n                result[k - 1] = n - k;\n            } else {\n                // Case 1: In ring pairs\n                if (k <= ring / 2) {\n                    result[k - 1] += k * 2 == ring ? ring / 2 : ring;\n                }\n\n                // Case 2: House pairs that is on the line without going into the ring\n                result[k - 1] += Math.max(0, k == 1 ? lenWithoutRing - k - 1 : lenWithoutRing - k);\n\n                // Case 3: One house out of the ring, the other house in the ring\n                if (k != 1) {\n                    int maxInRingLen = Math.min(k - 1, ring / 2);\n\n                    int leftMinInRingLen = Math.max(1, k - (x - 1));\n                    if (leftMinInRingLen <= maxInRingLen) {\n                        result[k - 1] += 2 * (maxInRingLen - leftMinInRingLen + 1);\n                        // Special case, in ring len is 1, which is overlapped with case 2\n                        if (leftMinInRingLen == 1) {\n                            result[k - 1]--;\n                        }\n                        // Special case, in ring len is ring/2 and ring is even, in this case there will\n                        // be one less pair\n                        if (maxInRingLen * 2 == ring) {\n                            result[k - 1]--;\n                        }\n                    }\n\n                    int rightMinInRingLen = Math.max(1, k - (n - y));\n                    if (rightMinInRingLen <= maxInRingLen) {\n                        result[k - 1] += 2 * (maxInRingLen - rightMinInRingLen + 1);\n                        if (rightMinInRingLen == 1) {\n                            result[k - 1]--;\n                        }\n                        if (maxInRingLen * 2 == ring) {\n                            result[k - 1]--;\n                        }\n                    }\n                }\n            }\n\n            result[k - 1] *= 2;\n        }\n\n        return result;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<long long> countOfPairs(int n, int x, int y) {\n        vector<long long> psa(n + 5);\n        if (x > y)\n            swap(x, y);\n        if (x == y) {\n            for (int i = 1; i <= n; i++) {\n                psa[1]++;\n                psa[i]--;\n                psa[1]++;\n                psa[n + 1 - i]--;\n            }\n        } else {\n            for (int i = 1; i <= n; i++) {\n                if (i <= x) {\n                    psa[1]++;\n                    psa[i]--; // [0 to i)\n                    psa[1]++;\n                    psa[x - i + 1]--; // (i to x]\n                    int rq = y - x;\n                    int v = rq / 2;\n                    int v2 = rq - v;\n                    psa[x - i + 1]++;\n                    psa[v + 1 + x - i]--;\n                    psa[x - i + 1]++;\n                    psa[v2 + 1 + x - i]--;\n                    // (x to y]\n                    psa[x - i + 2]++;\n                    psa[n + 1 - y + 1 + x - i]--; // (y to n]\n                } else if (i >= y) {\n                    psa[1]++;\n                    psa[n - i + 1]--;\n                    psa[1]++;\n                    psa[i - y + 1]--; // [y to i)\n\n                    int rq = y - x;\n                    int v = rq / 2;\n                    int v2 = rq - v;\n                    psa[i - y + 1]++;\n                    psa[i - y + v + 1]--;\n                    psa[i - y + 1]++;\n                    psa[i - y + v2 + 1]--;\n\n                    psa[i - y + 2]++;\n                    psa[x + 1 + i - y]--;\n                    // [1 , x)\n\n                } else {\n                    int rq = y - x;\n                    int v = rq / 2;\n                    int v2 = rq - v;\n                    psa[1]++;\n                    psa[v + 1]--;\n                    psa[1]++;\n                    psa[v2 + 1]--;\n                    int dx = min(i - x, y - i + 1);\n                    int dy = min(y - i, i - x + 1);\n                    psa[dx + 1]++;\n                    psa[dx + x]--;\n                    psa[dy + 1]++;\n                    psa[dy + n - y + 1]--;\n                }\n            }\n        }\n\n        vector<long long> ans;\n        for (int i = 1; i <= n; i++) {\n            psa[i] += psa[i - 1];\n            ans.push_back(psa[i]);\n        }\n        return ans;\n    }\n};\n```\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public long[] countOfPairs(int n, int x, int y) {\n        if (x > y) {\n            return countOfPairs(n, y, x);\n        }\n\n        if (x + 1 == y) {\n            return countOfPairs(n, x, x);\n        }\n\n        int lenWithoutRing = n - (y - x) + 1;\n        int ring = y - x + 1;\n        long[] result = new long[n];\n        for (int k = 1; k < n; k++) {\n            if (x == y) {\n                result[k - 1] = n - k;\n            } else {\n                // Case 1: In ring pairs\n                if (k <= ring / 2) {\n                    result[k - 1] += k * 2 == ring ? ring / 2 : ring;\n                }\n\n                // Case 2: House pairs that is on the line without going into the ring\n                result[k - 1] += Math.max(0, k == 1 ? lenWithoutRing - k - 1 : lenWithoutRing - k);\n\n                // Case 3: One house out of the ring, the other house in the ring\n                if (k != 1) {\n                    int maxInRingLen = Math.min(k - 1, ring / 2);\n\n                    int leftMinInRingLen = Math.max(1, k - (x - 1));\n                    if (leftMinInRingLen <= maxInRingLen) {\n                        result[k - 1] += 2 * (maxInRingLen - leftMinInRingLen + 1);\n                        // Special case, in ring len is 1, which is overlapped with case 2\n                        if (leftMinInRingLen == 1) {\n                            result[k - 1]--;\n                        }\n                        // Special case, in ring len is ring/2 and ring is even, in this case there will\n                        // be one less pair\n                        if (maxInRingLen * 2 == ring) {\n                            result[k - 1]--;\n                        }\n                    }\n\n                    int rightMinInRingLen = Math.max(1, k - (n - y));\n                    if (rightMinInRingLen <= maxInRingLen) {\n                        result[k - 1] += 2 * (maxInRingLen - rightMinInRingLen + 1);\n                        if (rightMinInRingLen == 1) {\n                            result[k - 1]--;\n                        }\n                        if (maxInRingLen * 2 == ring) {\n                            result[k - 1]--;\n                        }\n                    }\n                }\n            }\n\n            result[k - 1] *= 2;\n        }\n\n        return result;\n    }\n}\n```\n# Please upvote if it\'s helpful .. your like motivates me to do better..\n![th.jpeg](https://assets.leetcode.com/users/images/ec6dd0bd-6d46-4615-a6cf-d21930a37a87_1705843359.4018319.jpeg)\n
['C++', 'Java']