O(n^2) || ONLY MATH || NO GRAPH || 🔥🔥🔥
Intuition\nThe distance between any two houses is dist(u,v) = |v - u|. The addition of new edge between (x,y) creates a new path to reach all the nodes in  newD
# Intuition\nThe distance between any two houses is $$dist(u,v) = |v - u|$$. The addition of new edge between $$(x,y)$$ creates a new path to reach all the nodes in $$ newDist(u,v) = dist(u,x) + dist(x,y) + dist(y,v)$$. So the new minimum distance is just the minimum of the two.\n\n# Approach\n1. Create a dist matrix and store all distances without the new edge $$(x,y)$$\n2. create an answer vector.\n3. go through every pair of house  ,calculate the new minimum distance.\n4. increment the ans $$ index = minDistance$$  \n\n# Complexity\n- Time complexity:\n$$O(N^2)$$\n\n- Space complexity:\n    $$O(N^2)$$\n\n# Code\n```\nconst int INF = 1e9;\n\nclass Solution {\npublic:\n    vector<int> countOfPairs(int n, int x, int y) {\n        vector<vector<int>> dist(n+1 , vector<int>(n+1,INF));\n\n        if(x>y) swap(x,y);\n\n        for(int i = 1 ;i<=n ;i++){\n            for(int j = i; j<=n ;j++){\n                dist[i][j] = j - i;\n                dist[j][i] = dist[i][j];\n            }\n        }\n\n        dist[x][y] = x!=y;\n        dist[y][x] = x!=y;\n\n        vector<int> ans(n,0);\n        for(int i = 1 ;i<=n ;i++){\n            for(int j = i; j<=n ;j++){\n                int new_dist = dist[i][x] + dist[x][y] + dist[y][j];\n                if(new_dist < dist[i][j]){\n                    if(new_dist==0) continue;\n                    ans[new_dist-1]+=2;\n                }else{\n                    if(dist[i][j]==0) continue;\n                    ans[dist[i][j]-1]+=2;\n                }\n            }\n        }\n\n        return ans;\n    }\n};\n```
['Math', 'Matrix', 'C++']