i am writing the function to check if the given value is included in the matrix in . This matrix has the following properties: •Integers in each row are sorted in ascending from left to right. •Integers in each column are sorted in ascending from top to bottom
public static boolean searchMatrix(int target, int[][] matrix)
{
for(int i = 0;i <= matrix[0].length;i++)
{
for (int j=i; j<matrix.length;j++)
{
if(target == matrix[i][j])
return true;
}
}
return false;
}
i want to know weather this program has time complexity of O(N). if not what changes should i make to get into O(N).
O(1), one loop isO(n)and 2 loops with one nested isO(n^2)orO(n*m).n is the size of the double array row*columnso even though with nested loop wouldnt it beO(n)because the loop will loopntime?Nis, but generally you want it something that scales lineally. Stating thatN=row*colmeans that while you could state it'sO(N), it's not liner to the size of the matrices row and columns.