Search a 2D Matrix

MediumBinary Search
Next
Binary SearchMatrix

A matrix has two properties: each row is sorted left to right, and the first number of every row is greater than the last number of the previous row. Decide whether a target value exists in it, in O(log(rows * cols)).

Input

Line 1: r, c and the target. Next r lines: c integers each.

Output

Print true if the target is in the matrix, otherwise false.

Constraints

1 <= r, c <= 200 -10^9 <= each value, target <= 10^9

Example 1
Input
3 4 3
1 3 5 7
10 11 16 20
23 30 34 60
Output
true

3 is in the first row.

Example 2
Input
3 4 13
1 3 5 7
10 11 16 20
23 30 34 60
Output
false

13 falls between rows and is not present.

Hints

Search a 2D Matrix - Medium Binary Search Problem | Skill Shiksha