Be the first user to complete this post

  • 0
Add to List
Medium

8. Efficient Search in 2D Sorted Array: Find an Element

Objective: Write an algorithm to find an element in 2-dimensional array where rows and columns are sorted respectively.

Input: A two dimensional sorted array, arrA[][].
Output: True or false based on whether the element exists

Approach:

  • Start from the right top corner, say element is the matrix element;
  • If element >number -> move left
  • If element <number -> move down
  • If cant move further to find the number , return false
Find element in Sorted 2D array
The Movement : 4->9->16->15->The number 15 present : true
The Movement : 4->9->8->7->6->The number 5 present : false