Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Time complexity of searching for target value in 2D matrix?

The actual problem is pretty simple, implement an algorithm that returns true if the target value is contained within the matrix. Here are the two solutions I came up with. I'm not sure which one would be preferable? I believe solution 1 is faster since we don't need to build a new array, however would this be significantly faster?

Solution 1:

var searchMatrix = function(matrix, target) {
    let cols = matrix[0].length;
    let rows = matrix.length;
    let left = 0; 
    let right = cols*rows - 1;
    
    while(left <= right) {
        let midIndex = left + Math.floor((right-left)/2);
        let midValue = matrix[Math.floor(midIndex/cols)][Math.floor(midIndex%cols)];
        console.log(midValue);
        if(midValue === target) {
            return true;
        }
        else if(midValue < target) {
            left = midIndex + 1;
        } else {
            right = midIndex - 1;
        }
    }
    return false;
};

Solution 2:

var searchMatrix = function(matrix, target) {
    let arr = []
    for(let row of matrix) {
        arr = [...arr,...row];
    }
    let left = 0;
    let right = arr.length - 1;
    
    while(left <= right) {
        let middle = left + Math.floor((right-left)/2);
        if(arr[middle] === target) {
            return true;
        } else if(arr[middle] < target) {
            left = middle + 1;
        } else {
            right = middle - 1;
        }
    }
        
    return false;
};

Based on my understanding, the main step we're adding is converting the matrix into a regular Array. Would this make the algorithm O(n) since we have to add every element into the new array?

For solution 1, we don't have to create a new array so we'd have constant space correct? I'm not particularly sure how to explain the first solution is preferable in terms of time/space.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Based on the comments added to the post, readability and the ability to analyze the time complexity would yield the best answer. Perhaps the first option would be most ideal if we can make it more readable via a separate function used to calculate the matrix value.

Option 2 does take more space since we're creating an additional array to search through.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!