Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- https://practice.geeksforgeeks.org/problems/search-in-a-matrix17201720/1
- Logic:
- remove a row or column in each comparison until an element is found.
- if top-right element is greater than x then we can't go downwards because
- whole column will have a greater elements so only one option left,go left
- side and same goes for each position of array.
- Time complexity: O(n+m)
- */
- class Solution{
- public:
- int matSearch (vector <vector <int>> &arr, int n, int m, int x){
- int i=0,j=m-1;
- while(i<n && j>=0){
- if(arr[i][j]>x){
- j--;
- }
- else if(arr[i][j]<x){
- i++;
- }
- else return 1;
- }
- return 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement