Easy To Understand || Floyd-Warshall Algorithm || Graphs || 🔥🔥
Code


# Code
```java []
import java.util.*;

class Solution {
    public int[] countOfPairs(int n, int x, int y) {
        // Initialize the grid with a large value (infinity)
        int[][] grid = new int[n + 1][n + 1];
        // for (int[] row : grid) {
        //     Arrays.fill(row, (int)(1e9));
                

        // }
                for (int i = 1; i <= n; i++) {
            Arrays.fill(grid[i],(int)(1e9) );
            grid[i][i] = 0; // Distance to itself is 0
        }


        // Set direct connections between adjacent nodes
        for (int j = 1; j < n; j++) {
            grid[j][j + 1] = 1;
            grid[j + 1][j] = 1;
        }
        // Set the special direct connection between x and y
        grid[x][y] = 1;
        grid[y][x] = 1;

        // Floyd-Warshall algorithm to calculate shortest paths via(k)
        for (int via = 1; via <= n; via++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    grid[i][j] = Math.min(grid[i][j], grid[i][via] + grid[via][j]);
                }
            }
        }

        // Create a result array to store counts of pairs for each path length
        int[] result = new int[n];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i != j) {
                    int val = grid[i][j];
                    result[val - 1]++;
                }
            }
        }

        return result;
    }
}

```
['Graph', 'Shortest Path', 'Java']