Time Complexity of Nested Loops
The process method is shown below. Assume that matrix is a 2D array with n rows and m columns, and that the maximum value of any element in matrix is k. What is the worst-case time complexity of the method?
public class VariableInnerLoop {
public static void process(int[][] matrix) {
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++){
for (int k = 0; k < matrix[i][j]; k++){
// constant operation
int dummy = k;
}
}
}
}
}
A
O(n + m + k)
B
O(n*m)
C
O(nmk)
D
O(n*m + k)
Question Leaderboard
Not enough data yet to show leaderboard.
