Given M x N matrix with sorted elements row wise and column wise, find elements?



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

Post New Answer

More Data Structures Interview Questions

Is bubble sort adaptive?

1 Answers  


What is merge sort in daa?

1 Answers  


How many pointers are necessary to implement a simple linked list?

1 Answers  


Explain the common uses of threaded binary tree.

1 Answers  


Can we override compareto method for enumerations?

1 Answers  


What is the logic to reverse the array?

1 Answers  


Can we increase the size of statically allocated array?

1 Answers  


Which is better bubble sort or selection sort?

1 Answers  


Is arraylist heterogeneous?

1 Answers  


What is an iterative algorithm?

1 Answers  


How many types of lists are there?

1 Answers  


Explain the steps to insert data into a singly linked list?

1 Answers  


Categories