Careful casework (Python)
Approach\nObviously three groups, but the casework is super tricky, so you have to be careful.\nA good start (in a contest) is writing a checker so that you don
# Approach\nObviously three groups, but the casework is super tricky, so you have to be careful.\nA good start (in a contest) is writing a checker so that you don\'t get a bunch of WAs like I did.\nAlso each pair (i, j) being counted twice is kind of weird.\n\n$$O(n)$$\n\n![ss.png](https://assets.leetcode.com/users/images/599cfdd3-a609-4cf2-8ab2-e30c56f69a23_1705810903.607305.png)\n\n## Don\'t forget odd and even cycle size cases!\n\n![ss2.png](https://assets.leetcode.com/users/images/b12bcabb-b4b4-4a60-b6c6-cbeeb9977f8c_1705811029.8117378.png)\n\n# Code\n```py\nclass Solution:\n    def countOfPairs(self, N: int, x: int, y: int) -> List[int]:\n        x -= 1\n        y -= 1\n        x, y = sorted((x, y))\n        \n        w = abs(x-y)+1\n        res = [0]*N\n        \n        # Linking within a cycle\n        # Fill (w-1)/2 nodes with count w*2\n        # Think about the opposite node in the cycle (draw it out please)\n        for i in range(w//2): res[i] += w\n        for i in range((w-1)//2): res[i] += w\n        \n        # Linking within l and r groups\n        l = x-0\n        r = N-1-y\n        for k in range(0, l): res[k] += 2*(l-k)\n        for k in range(0, r): res[k] += 2*(r-k)\n        \n        # These next two for-loops pair l nodes connected to\n        # a pivot node (l not including pivot), which is then connected to\n        # a cycle of length w (w including pivot)\n        # \n        # +3 is just for safety\n        for k in range(2, l+3+w//2):\n            rs = min(-1, (w-1)/2-k)  # Float division again for opposite node in the cycle\n            ls = max(1-k, -l)\n            cur = max((rs-ls+1)*4, 0)  # Decimal disappears with the *4 here\n            if cur:\n                res[k-1] += int(cur)  # (int doesn\'t actually truncate here, only removes .0)\n        \n        for k in range(2, r+3+w//2):\n            rs = min(-1, (w-1)/2-k)\n            ls = max(1-k, -r)\n            cur = max((rs-ls+1)*4, 0)\n            if cur:\n                res[k-1] += int(cur)\n        \n        # connecting l and r groups through the cycle shortcut\n        for k in range(2, l+3+r):\n            rs = min(-1, r-k)\n            ls = max(1-k, -l)\n            cur = max(rs-ls+1, 0)*2\n            if cur:\n                res[k-1+min(1, w-1)] += cur  # min(1, w-1) here to deal with self loops\n        \n        return res\n```
['Counting', 'Python3']