Search a globally sorted matrix
Return whether target appears in a rectangular matrix whose rows are sorted and each row begins after the previous row ends. Use logarithmic comparisons.
flat_index, columns = 4, 3
print(divmod(flat_index, columns))Reason about the state
View the matrix as one sorted sequence. Convert a flat index into row and column with division and remainder during binary search. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return whether target appears in a rectangular matrix whose rows are sorted and each row begins after the previous row ends. Use logarithmic comparisons.
The explanation opens here once every check passes.