diff --git a/merge-sorted-array.cpp b/merge-sorted-array.cpp new file mode 100644 index 00000000..e69de29b diff --git a/remove-duplicates.cpp b/remove-duplicates.cpp new file mode 100644 index 00000000..e69de29b diff --git a/search-2d-matrix.cpp b/search-2d-matrix.cpp new file mode 100644 index 00000000..eae2a117 --- /dev/null +++ b/search-2d-matrix.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool searchMatrix(vector>& matrix, int target) { + int m = matrix.size(); + int n = matrix[0].size(); + int i = 0; + int j = n - 1; + while(i < m && j >=0){ + if(matrix[i][j] == target)return true; + else if(matrix[i][j] > target){ + j--;//move towards the smaller elements + }else{ + i++;//move towards the larger elements + } + } + + return false;//element not found + } +}; \ No newline at end of file