out of three only one test case run… where is the problem
Matrix search problem
@Amre-8800,
https://ide.codingblocks.com/s/240613 corrected code.
We will make use of the fact that matrix is row wise and column wise sorted. So if we start from the top right we can observe the fact that if we go in the left direction the numbers decrease and if we go downwards the numbers increase. So we can restrict our search space depending on which index we are currently at. If the number at which we are currently standing is greater than the target number then it is obvious that we can find it on the left side of the current row and if the target is greater than the element at current standing then it is obvious that we can find it towards the bottom in that column where every number is greater.
1.Let array be arr and target be the element to be found.
2.Iterate over each row
2.1 Iterate over each column in reverse order
2.1.1 If arr[row][col]==target then break the loop and print 1
2.1.2 if arr[row][col]>target then
col--; //decrement columns
2.1.3 else row++; //increment rows
3.If loop is not broken in between then it indicates that element is not present in arr .So print 0.
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.