[Java] Pure math solution
Intuition\n\nLet\'s think of the problem in a differet way: there is a line, and on the line, there is a ring. In below example, the line is 1 2 3 8 9, and the 
# Intuition\n\nLet\'s think of the problem in a differet way: there is a line, and on the line, there is a ring. In below example, the line is `1 2 3 8 9`, and the ring is `3 4 5 6 7 8`. It represents the input `n = 9, x = 3, y = 8`.\n\n![Screenshot 2024-01-20 225421.png](https://assets.leetcode.com/users/images/c8f23cef-4ba1-42d2-8c2b-23858a1655f2_1705820089.3718734.png)\n\n\nThen it is not hard to see that there are 3 main cases for the valid house pairs (excluding the edge cases like `x == y` or `x + 1 == y` or `x - 1 == y`).\n\n**Case 1: In ring pairs**\n\nThe two houses are both in the ring. This case is straightforward. As long as `k` is smaller than half of the ring, we can easily count the number of valid pairs.\n\nExample: `(3,5), (4,6), (5,7), (6,8), (7,3), (8,4) for k == 2`.\n\n**Case 2: In line pairs**\n\nThe two houses are both in the line. This case is also straightforward. The only thing to note is when `k == 1` it overalaps with case 1.\n\nExample: `(1,3), (2,8), (3,9) for k == 2`.\n\n**Case 3: One house out of the ring, the other house in the ring**\n\nThis is the tricky part. We can make use of the restrictions of the in-ring length, i.e., the length of the in-ring streets to travel from one house to the other.\n\nFirst we calculate the max in-ring length, which should be `min(k-1, ring/2)` (no more than k-1, and no more than ring/2).\n\nThen we calculate the min in-ring length based on the length of the left line/tail, which should be `max(1, k-(x-1))` for the left side (at least 1, at least k-(x-1)).\n\nIf the second number is smaller than the first one, there are pairs under this case. We need to handle some special cases here as well... Check the code for details.\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 line = 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 ? line - k - 1 : line - 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 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```
['Java']