SPACE OPTIMIZED SOLUTION
PLS UPVOTE MY SOLUTION IF YOU LIKE IT AND COMMENT FOR ANY DOUBTS \n# Approach\nInitialize startrow to 0, representing the first row of the matrix.\nInitialize s
# **PLS UPVOTE MY SOLUTION IF YOU LIKE IT AND COMMENT FOR ANY DOUBTS **\n# Approach\nInitialize startrow to 0, representing the first row of the matrix.\nInitialize startcol to m-1, representing the last column of the matrix. m is the number of columns in the matrix.\nStart a while loop that continues until startrow is less than the number of rows (n) and startcol is greater than or equal to 0.\nInside the loop, retrieve the element at the current position in the matrix using matrix[startrow][startcol] and store it in the variable ele.\nCompare ele with the target value. If they are equal, the target is found, and the function returns true (1).\nIf ele is less than the target, it means the target can only be present in a row below the current one (since the matrix is sorted in non-decreasing order). So, we increment startrow to move to the next row.\nIf ele is greater than the target, it means the target can only be present in a column to the left of the current one. So, we decrement startcol to move to the previous column.\nIf the target is not found after the while loop, the function returns false (0).\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:0(m+n) WORST CASE\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    bool searchMatrix(vector<vector<int>>& matrix, int target) {\n        int n = matrix.size();\n        int m = matrix[0].size();\n        int startrow=0;\n        int startcol= m-1;\n        while(startrow < n && startcol >= 0)\n        {\n            int ele = matrix[startrow][startcol];\n            if(ele==target)\n            {\n                return 1;\n            }\n            else if(ele<target)\n            {\n                startrow++;\n            }\n            else\n            {\n                startcol--;\n            }\n\n        }\n        return 0;\n    }\n};\n```
['Array', 'Matrix', 'C++']