Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Searching in a sorted 2-D array in JavaScript
We are required to write a JavaScript function that takes in an array of arrays of numbers as the first argument and a number as the second argument. The subarrays contain numbers sorted in an increasing order and no element of a preceding subarray is greater than any element of the succeeding subarray.
The function should use the binary search algorithm to search for the element provided as the second argument in the sorted array of arrays.
If the element exists the function should return true, false otherwise.
For example −
If the input array is −
const arr = [ [2, 6, 9, 11], [13, 16, 18, 19, 21], [24, 26, 28, 31] ]; const num = 21;
Then the output should be −
const output = true;
Algorithm Approach
The algorithm works in two phases:
- Find the target row: Iterate through each row to find which row might contain the target number by checking if the target falls within the row's range (first element ? target ? last element).
- Binary search within row: Once the potential row is found, perform binary search within that row to locate the target element.
Example
Following is the code −
const arr = [
[2, 6, 9, 11],
[13, 16, 18, 19, 21],
[24, 26, 28, 31]
];
const num = 21;
const search2D = (array = [], target) => {
const h = array.length;
const w = h > 0 ? array[0].length : 0;
if (h === 0 || w === 0) {
return false;
}
const arr = getArr();
if (!arr) {
return false;
}
return binarySearch(arr, target) !== null;
function getArr() {
for (let i = 0; i < h; i++) {
let arr = array[i];
if (arr[0] <= target && target <= arr[arr.length - 1]) {
return arr;
}
}
return null;
}
function binarySearch(arr, t) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
if (arr[left] === t) {
return left;
}
if (arr[right] === t) {
return right;
}
let mid = Math.floor((left + right) / 2);
if (arr[mid] === t) {
return mid;
}
if (arr[mid] < t) {
left = mid + 1;
} else if (arr[mid] > t) {
right = mid - 1;
}
}
return null;
}
};
console.log(search2D(arr, num));
true
Testing with Different Values
const arr = [
[2, 6, 9, 11],
[13, 16, 18, 19, 21],
[24, 26, 28, 31]
];
// Test various cases
console.log("Search for 21:", search2D(arr, 21)); // true - exists in second row
console.log("Search for 2:", search2D(arr, 2)); // true - first element
console.log("Search for 31:", search2D(arr, 31)); // true - last element
console.log("Search for 15:", search2D(arr, 15)); // false - doesn't exist
console.log("Search for 0:", search2D(arr, 0)); // false - too small
console.log("Search for 50:", search2D(arr, 50)); // false - too large
Search for 21: true Search for 2: true Search for 31: true Search for 15: false Search for 0: false Search for 50: false
Time Complexity
The time complexity is O(m + log n) where:
- m is the number of rows (linear search to find target row)
- n is the number of columns (binary search within the row)
This is more efficient than searching each element individually, which would be O(m × n).
Conclusion
This approach efficiently searches a sorted 2D array by first identifying the target row, then using binary search within that row. The algorithm leverages the sorted properties of both rows and the overall matrix structure to achieve better than linear time complexity.
