Recently I was solving a question on codeforces. On coding its solution at the end I had to print the values from a 2 dimensional array for which I used,
for (int p = 0; p < m; p++) {
if (positions[edges[p][0]-1] < positions[edges[p][1]-1]) {
System.out.printf("%d %d\n", edges[p][0], edges[p][1]);
} else {
System.out.printf("%d %d\n", edges[p][1], edges[p][0]);
}
}
Using the above approach caused a Time Limit Exceed on a test case. While I replaced the above code to the one like the following, it worked.
for (int p = 0; p < m; p++) {
if (positions[edges[p][0]-1] < positions[edges[p][1]-1]) {
System.out.println((edges[p][0]) + " " + (edges[p][1]));
} else {
System.out.println((edges[p][1]) + " " + (edges[p][0]));
}
}
What could have caused this?
My submission for reproducing here