Logn 2 Dimensional Array Binary Search

Problem

Given a square two dimensional integer array of size (n). Both rows and columns are sorted in ascending order. Give an algorithm to search for a particular element in the array in O(Log(n)) time complexity. Assume the array has no duplicates.

Solution

This problem is a follow up from the previous post. Modified binary search can be used to search the array in O(Log(n)). The trick is how to calculate the left, middle and right elements. You can refer to the code below for the details.

Code

Here is the code in C#

Add a Comment

Your email address will not be published. Required fields are marked *