Jobs
Interviews

Solve Search in Sorted Matrix using Ruby Language

Solve Search in Sorted Matrix using Ruby to enhance your skills with ruby coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Search in Sorted Matrix

Difficulty : Medium

Categories :

  • Searching algorithms

Given a 2D matrix of integers where each row is sorted in ascending order and the first element of each row is greater than the last element of the previous row, implement a function to search for a target value. Return [row, col] if found, or [-1, -1] if not found.

Constraints:

  • 1 ≤ matrix.length, matrix[i].length ≤ 100
  • -1000 ≤ matrix[i][j] ≤ 1000
  • Each row is sorted in ascending order
  • First element of row is greater than last element of previous row
  • Must achieve better than O(n*m) time complexity

Examples:

Input:
matrix = [
 [1,3,5,7],
 [10,11,16,20],
 [23,30,34,60]
]
target = 3
Output: [0,1]
Explanation: Target 3 found at row 0, column 1
Input:
matrix = [
 [1,3,5],
 [7,9,11]
]
target = 4
Output: [-1,-1]
Explanation: Target 4 not found

Problem Solving

Input

What You'll Find Here

Real-World Scenarios Solve problems inspired by common Ruby use cases.

Step-by-Step Guidance Understand the core concepts of Ruby through clear explanations.

Practical Skills Prepare for real-world challenges with hands-on coding exercises.

Choose from the following categories