Easy BFS Java Solution
Plain Old BFS from each node\n# Complexity\n- Time complexity: O(N*N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(N)\n Add your space co
Plain Old BFS from each node\n# Complexity\n- Time complexity: O(N*N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    int res[];\n    public int[] countOfPairs(int n, int x, int y) {\n        res = new int[n];\n        List<List<Integer>> graph = grahBuilder(n,x,y);\n        for(int i=1; i<n+1; i++) {\n            bfs(graph, i, n);\n            //System.out.println("-----------END------------");\n        }\n        return res;\n    }\n\n    public List<List<Integer>> grahBuilder(int n, int x, int y) {\n\n        List<List<Integer>> graph = new ArrayList<>();\n\n        for(int i=0; i<n+1; i++)\n            graph.add(new ArrayList<>());\n\n        for(int i=1; i<n; i++) {\n            graph.get(i).add(i+1);\n            graph.get(i+1).add(i);\n        }\n\n        graph.get(x).add(y);\n        graph.get(y).add(x);\n\n        return graph;\n    }\n\n    public void bfs(List<List<Integer>> graph, int node, int n) {\n        \n        Queue<Integer> queue = new LinkedList<>();\n        queue.offer(node);\n        int count = 0;\n        boolean visited[] = new boolean[n+1];\n        visited[node] = true;\n\n        while(!queue.isEmpty()) {\n            count++;\n            int len = queue.size();\n            for(int i=0; i<len; i++) {\n                int ver = queue.poll();\n                List<Integer> list = graph.get(ver);\n                for(int adj:list) {\n                    if(!visited[adj]) {\n                        visited[adj] = true;\n                        //System.out.println(node + " --> " + adj + "-count-" + count + "--parent--" + ver);\n                        res[count-1]++;\n                        queue.offer(adj);\n                    }\n                }\n            }\n        }\n    }\n\n}\n```
['Breadth-First Search', 'Graph', 'Prefix Sum', 'Java']