🚀 [cpp] | bfs
\n\n\nclass Solution {\npublic:\n    vector<int> countOfPairs(int n, int x, int y) {\n        // build graph\n        vector<vector<int>> adj(n + 1);\n        i
\n\n```\nclass Solution {\npublic:\n    vector<int> countOfPairs(int n, int x, int y) {\n        // build graph\n        vector<vector<int>> adj(n + 1);\n        if(x != y) { adj[x].push_back(y); adj[y].push_back(x);}\n        for(int i = 1; i < n; i++) { adj[i].push_back(i + 1); adj[i + 1].push_back(i);}\n        \n        vector<int> ans(n);\n        \n        // bfs\n        auto bfs = [&](int u) -> void {\n            queue<int> q;\n            q.push(u);\n            \n            vector<int> seen(n + 1);\n            seen[u] = 1;\n            \n            int k = 0; // level\n            while(!q.empty()) {\n                for(int sz = q.size(); sz > 0; sz--) {\n                    int x = q.front(); q.pop();\n                    for(int v : adj[x]) {\n                        if(!seen[v]) {\n                            q.push(v); \n                            seen[v] = 1; \n                            ans[k]++;\n                        }\n                    }\n                }\n                k++;\n            }\n           \n        };\n        \n        // apply bfs from each house\n        for(int house = 1; house <= n; house++) bfs(house);\n        \n        return ans;\n    }\n};\n```
['C++']