Multi-Source BFS | Brute Force | C++ - Simple code
Intuition\nQuestion says we have points connected to some other points and we have to return the number of ways we have to reach from (point1 to point2) using k
# Intuition\nQuestion says we have points connected to some other points and we have to return the number of ways we have to reach from (point1 to point2) using k number of steps.\n\n\n\n# Approach\n\nApply BFS traversal to see where you reach at kth traveral and store it in a array\n\n# Complexity\n- Time complexity:\n O(n * (n + m)),n is the number of nodes & m is the number of edges.\n\n- Space complexity:\nO(n + m)\n\n# Code\n```\n\nclass Solution {\npublic:\n    \nlong long steps[101];\n    \n\nvoid bfs(int node,unordered_map<int,vector<int>> &mp)\n{\n    int ans =0;\n    int step = 0;\n    queue<int> q;\n    set<int> visited;\n    q.push(node);\n    visited.insert(node);\n    \n    while(!q.empty())\n    {\n        int size = q.size();\n        step ++;\n        while(size--)\n        {\n          int curr = q.front();\n            q.pop();\n        \n        \n        for(auto x:mp[curr])\n        {\n            if(visited.find(x) == visited.end())\n            {\n                q.push(x);\n                visited.insert(x);\n            }\n             \n        }\n        \n        \n          \n        }\n\n        steps[step] += q.size(); \n    }\n}\nvoid solve(unordered_map<int,vector<int>> &mp,int n)\n{\n    for(int i=1;i<=n;i++)\n    {\n        bfs(i,mp);\n    }\n}\n\n    vector<int> countOfPairs(int n, int x, int y) {\n             memset(steps,0,sizeof(steps));\n            unordered_map<int,vector<int>> mp;\n            \n        for(int i=1;i<=n-1;i++)\n        {\n            int curr = i;\n            int next = i+1;\n            \n            mp[curr].push_back(next);\n            mp[next].push_back(curr);\n        }\n        int temp = 0;\n        if(x!=y){\n            mp[x].push_back(y);\n            mp[y].push_back(x);\n        }\n        \n        vector<int> ans(n,0);\n        solve(mp,n);\n        for(int i=0;i<n;i++)\n        {\n            ans[i] = steps[i+1];\n        }\n        return ans;\n    }\n};\n```
['C++']