while accessing all elements of a matrix we can use to for loops and time complexity is O(n^2)
.
But, when we are only accessing rows from a 2D array, what is the time complexity? Is it O(n)
when n = number of rows?
Ex:-
int arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
The time complexity for the above lines is O(n^2)
.
But if we access the matrix in the following way will the time complexity still be O(n^2)
or will it be O(n)
for (int rows[] : arr) - Time complexity is O(n) or O(n^2)