Assuming that the distances between cities are represented in a 2D array or matrix, where both the rows and columns correspond to the cities and the values in the cells indicate the distances between them, you can tackle this problem in the following manner: First, loop through each cell in the upper triangle of the matrix (excluding the diagonal). The rationale for not examining the entire matrix is that the distances are typically symmetric (meaning the distance from city A to city B is equal to the distance from city B to city A), allowing you to eliminate unnecessary calculations. Next, calculate the total of these distances while keeping track of how many distances you have included. Finally, determine the average by dividing the total sum by the count of distances. In Python, this could be implemented as shown below: def average_distance ( matrix ) : total_sum = 0 count = 0 n = len ( matrix ) for i in range ( n ) : for j in range ( i + 1 , n ) : total_sum += matrix [ i ] [ j ] count += 1 return total_sum / count if count != 0 else 0