Given M x N matrix with sorted elements row wise and column wise, find elements?
Answer / Sanjo Devi
A matrix with sorted elements row-wise and column-wise can be searched using binary search for each row and each column. The element to find should be within the bounds of both the row and the column.nHere's an implementation in Python:n```pythonndef find_element(matrix, target):n m, n = len(matrix), len(matrix[0])n for i in range(m):n if matrix[i][0] <= target and matrix[i][-1] >= target:n start, end = 0, n - 1n while start <= end:n mid = (start + end) // 2n if matrix[i][mid] == target:n return f'Element found at row {i}, column {mid}'n elif matrix[i][mid] < target:n start = mid + 1n else:n end = mid - 1n if start >= n or end <= 0:n return f'Element not found in row {i}'n return 'No such element exists in the matrix'
| Is This Answer Correct ? | 0 Yes | 0 No |
Is bubble sort adaptive?
What is merge sort in daa?
How many pointers are necessary to implement a simple linked list?
Explain the common uses of threaded binary tree.
Can we override compareto method for enumerations?
What is the logic to reverse the array?
Can we increase the size of statically allocated array?
Which is better bubble sort or selection sort?
Is arraylist heterogeneous?
What is an iterative algorithm?
How many types of lists are there?
Explain the steps to insert data into a singly linked list?