C++ | BFS | Easy to understand
Approach\n- Do BFS from each node and maintain count\n\n# Code\n\nclass Solution {\npublic:\n    vector<int> countOfPairs(int n, int x, int y) {\n        vector
# Approach\n- Do BFS from each node and maintain count\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> countOfPairs(int n, int x, int y) {\n        vector<int> adj[n+1];\n        vector<int> res(n,0);\n\n        for(int i=2;i<=n;i++){\n            adj[i].push_back(i-1);\n            adj[i-1].push_back(i);\n        }\n        adj[x].push_back(y);\n        adj[y].push_back(x);\n\n        for(int i=1;i<=n;i++){\n            queue<int> q;\n            q.push(i);\n            vector<int> vis(n+1,0);\n            vis[i]=1;\n            int ctr=0;\n            while(!q.empty()){\n                int size=q.size();\n                ctr++;\n                for(int i=0;i<size;i++){\n                    int node=q.front();\n                    q.pop();\n                    for(auto it : adj[node]){\n                        if(!vis[it]){\n                            vis[it]=1;\n                            q.push(it);\n                            res[ctr-1]++;\n                        }\n                    }\n                }\n            }\n        }\n\n        return res;\n    }\n};\n\nstatic const int speedup = []() {\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n    cout.tie(nullptr);\n    return 0;\n}();\n```
['C++']