Search an element in unsorted 2d array

I am stuck with one program.
Problem : Given an NxN matrix, matrix is not sorted. Find the index of given element in the matrix.
Please explain with the code

if array is not sorted then you have to run 2 loop as check one by one
time complexity will be O(n^2)
Code

but if array is sorted then you can reduce time complexity

what if you have Q queries and each line of query contains one element to search. In this case for each element in query, TC will be O(n^2). Is there any optimal approach

you can sort the array once in n*log(n)
and the search in log(n)

@asaurabh_26 agar sort kr diye toh index ka dikkhat ayegi
We need to return the index

then use brute force approach

chalo…ye bhi theek hai

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.