Time limit exceeding

Hi can you please tell me what is wrong with my code. What is my time complexity and what it should really be?
What should I change in my code?

hello @vaani787
if u want to do it o(N*M) then simply use two nested loops and check every elements of matrix. why u are making its implementation complex?

the matrix search algorithm has different algortihm and it works in O(N+M) time complexity. and in that all u need is a single while loop.

check this in case algorithm ->

  1. Let the given element be x, create two variable i = 0, j = n-1 as index of row and column
  2. Run a loop until i = 0
  3. Check if the current element is greater than x then decrease the count of j. Exclude the current column.
  4. Check if the current element is less than x then increase the count of i. Exclude the current row.
  5. If the element is equal then print the position and end.
    https://ide.codingblocks.com/s/432620

Even for O(m*n) can you help debug my code. I believe it’s correct but can’t pass all test cases

yeah please explain what exaclty u want to do in ur code. that will help me in debugging

I am still getting the same error

@vaani787
i checked ur code it is correct.

1 Like

Okay, Thanks!
Btw, what was the time complexity of the approach i was using before? with while and for loop? How was it O(m*n)?

outer loop can run o(N) Or O(M) in worst case and for each iteration of outer loop the inner loop can run O(n) or O(M) so in worst case ur algorithm time complexity can be O(N*M)

Okay! So what will be the test case I should consider for the worst case scenario?

when key will not be present in matrix that will be your worst case

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.