369 numbers time limit exceeded

I have written this code

i am getting timelimit error but according to me this question would require atleast o(n2) complexity.
where am i getting it wrong??

@anishmittal hey ,it can be solved in less than O(n2)->

  • Approach: The simple idea is to remove a row or column in each comparison until an element is found. Start searching from the top-right corner of the matrix. There are three possible cases.
    1. The given number is greater than the current number: This will ensure, that all the elements in the current row are smaller than the given number as the pointer is already at the right-most element and the row is sorted. Thus, the entire row gets eliminated and continue the search on the next row. Here elimination means that row needs not to be searched.
    2. The given number is smaller than the current number: This will ensure, that all the elements in the current column are greater than the given number. Thus, the entire column gets eliminated and continue the search on the previous column i.e. the column at the immediate left.
    3. The given number is equal to the current number: This will end the search.The search can also be started from the bottom left corner of the matrix.
  • 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.

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.