Time Complexity Of Nested Loops
What is the time complexity of the following code segment? Assume arr is a 2D array with n rows and m columns.
public class NestedLoop {
public static void main(String[] args) {
int[][] arr = new int[n][m]; // Array has n rows and m columns
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
for (int k = 0; k < j; k++) {
// Constant-time operation
int temp = arr[i][j] + k;
}
}
}
}
}
A
O(nmlog m)
B
O(n*m²)
C
O(n²*m)
D
O(n*m)
Question Leaderboard
Not enough data yet to show leaderboard.
