C++ | O(N) Solution | Beats 100% Solution in Time & Memory
\n\n\n# Code\n\nclass Solution {\npublic:\n    vector<long long> countOfPairs(int n, int x, int y) {\n        if (x > y) {\n            swap(x, y);\n        }\n
![image.png](https://assets.leetcode.com/users/images/9c9d154d-c168-4428-975a-3e040e5ffa7d_1705810950.5972164.png)\n\n\n# Code\n```\nclass Solution {\npublic:\n    vector<long long> countOfPairs(int n, int x, int y) {\n        if (x > y) {\n            swap(x, y);\n        }\n        \n        vector<long long> res(n + 2);\n        if (x == y) {\n            vector<long long> ans(n);\n            for (int sz = 1, cnt = n - 1; cnt > 0; sz++, cnt--) {\n                ans[sz - 1] += 2LL * cnt;\n            }\n            return ans;\n        }\n        \n        auto getCircleDist = [&]() -> vector<int> {\n            vector<int> res;\n            res.emplace_back(1);\n            int len = y - x + 1;\n            for (int i = 0; i < (len - 1) / 2; i++) {\n                res.emplace_back(2);\n            }\n            if (len % 2 == 0) {\n                res.emplace_back(1);\n            }\n            return res;\n        };\n        \n        auto addPairCircles = [&]() -> void {\n            vector<int> circleDist = getCircleDist();\n            int len = y - x + 1;\n            int sz = (int) circleDist.size();\n            for (int d = 0; d < sz; d++) {\n                res[d] += 1LL * len * circleDist[d];\n            }\n        };\n        \n        auto addOutsideAndCircle = [&]() -> void {\n            vector<int> circleDist = getCircleDist();\n            int lft = x - 1;\n            int rgt = n - y;\n            for (int len : {lft, rgt}) {\n                if (len == 0) {\n                    continue;\n                }\n                vector<long long> cnt(n + 2);\n                int sz = (int) circleDist.size();\n                for (int d = 0; d < sz; d++) {\n                    int l = min(d + 1, n + 1);\n                    int r = min(l + len, n + 1);\n                    cnt[l] += circleDist[d];\n                    cnt[r] -= circleDist[d];\n                }\n                for (int i = 1; i <= n; i++) {\n                    cnt[i] += cnt[i - 1];\n                    res[i] += cnt[i] * 2LL;\n                }\n            }\n        };\n        \n        auto addOutsidePair = [&]() -> void {\n            int lft = x - 1;\n            int rgt = n - y;\n            for (int len : {lft, rgt}) {\n                for (int sz = 1, cnt = len - 1; cnt > 0; sz++, cnt--) {\n                    res[sz] += 2 * cnt;\n                }\n            }\n            if (lft == 0 || rgt == 0) {\n                return;\n            }\n            if (lft > rgt) {\n                swap(lft, rgt);\n            }\n            for (int d = 3, cur = 1, cnt = 0; d <= n; d++, cur++) {\n                if (cur > rgt) {\n                    cnt--;\n                } else if (cur <= lft) {\n                    cnt++;\n                }\n                if (cnt <= 0) {\n                    break;\n                }\n                res[d] += cnt * 2LL;\n            }\n        };\n        \n        addPairCircles();\n        addOutsideAndCircle();\n        addOutsidePair();\n        vector<long long> ans(n);\n        for (int i = 0; i < n; i++) {\n            ans[i] = res[i + 1];\n        }\n        return ans;\n    }\n};\n```
['C++']