MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / matrixSearch

Function matrixSearch

kotlin/Binary Search/MatrixSearch.kt:1–20  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1fun matrixSearch(matrix: List<List<Int>>, target: Int): Boolean {
2 val m = matrix.size
3 val n = matrix[0].size
4 var left = 0
5 var right = m * n - 1
6 // Perform binary search to find the target.
7 while (left <= right) {
8 val mid = left + (right - left) / 2
9 val r = mid / n
10 val c = mid % n
11 if (matrix[r][c] == target) {
12 return true
13 } else if (matrix[r][c] > target) {
14 right = mid - 1
15 } else {
16 left = mid + 1
17 }
18 }
19 return false
20}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected