We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

**in c++:
so basically ,
Initialize res to 1. This variable will hold the result of the calculation.

Iterate through the routes vector. For each element in the vector, do the following:

Reset res to 1 at the beginning of each iteration of the outer loop.

Iterate through the towns using the inner loop (for (int j = 0; j < n - 1; j++)).

For each town, calculate the number of routes and update res as follows:

Calculate (res % mod) * (routes[j] % mod) to get the product of the current value of res and the number of routes between the current pair of towns. This is done modulo mod to prevent integer overflow.
Update res with the result of the above calculation.
After the inner loop finishes, res will contain the total number of routes between all towns.
Return res modulo 1234567 as the final result.**

int connectingTowns(int n, vector routes) {
int res = 1;
long mod=1234567;
for(int i=0;i

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

## Connecting Towns

You are viewing a single comment's thread. Return to all comments →

**in c++: so basically , Initialize res to 1. This variable will hold the result of the calculation.

Iterate through the routes vector. For each element in the vector, do the following:

Reset res to 1 at the beginning of each iteration of the outer loop.

Iterate through the towns using the inner loop (for (int j = 0; j < n - 1; j++)).

For each town, calculate the number of routes and update res as follows:

Calculate (res % mod) * (routes[j] % mod) to get the product of the current value of res and the number of routes between the current pair of towns. This is done modulo mod to prevent integer overflow. Update res with the result of the above calculation. After the inner loop finishes, res will contain the total number of routes between all towns. Return res modulo 1234567 as the final result.**

int connectingTowns(int n, vector routes) { int res = 1; long mod=1234567; for(int i=0;i