diff --git a/problem1.cpp b/problem1.cpp new file mode 100644 index 00000000..f75b1d2c --- /dev/null +++ b/problem1.cpp @@ -0,0 +1,46 @@ +// Time Complexity : O(log m*n), since we are performing binary search on m*n elements +// Space Complexity : O(1), no additional space used +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach in three sentences only +// The approach here is to think of this 2D matrix as a 1D array, without converting it explictly. +// We can set low = 0 and high = m*n-1 and perform binary search to find the target. +// Once we get mid, we need to convert it to row and column index using the formula r = mid / n & c = mid % n, then proceed with normal binary search. + +#include +#include +using namespace std; + +class Solution { +public: + bool searchMatrix(vector>& matrix, int target) { + int m = matrix.size(); // rows + int n = matrix[0].size(); // columns + + int low = 0; + int high = m*n-1; // total number of elements - 1 + + while(low <= high){ + int mid = low + (high-low)/2; + int r = mid / n; // calculate row-index + int c = mid % n; // calculate col-index + if(matrix[r][c] == target) { // standard binary search conditions + return true; + } else if(matrix[r][c] > target) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return false; // we did not find the target in the matrix + } +}; + +int main() { + Solution solution; + vector> matrix = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}; + cout<<(solution.searchMatrix(matrix, 3) ? "3 exists in the matrix." : "3 was not found in the matrix.")< +#include +using namespace std; + +class Solution { +public: + int search(vector& nums, int target) { + int low = 0; + int high = nums.size() - 1; + while(low <= high) { + int mid = low + (high - low)/2; + if(nums[mid] == target) { + return mid; + } + if(nums[mid] >= nums[low]) { // left half is sorted + if(nums[low] <= target and target < nums[mid]) { // lies in sorted left half + high = mid - 1; + } else { // lies in the right half + low = mid + 1; + } + } else { // right half is sorted + if(nums[mid] < target && target <= nums[high]) { // lies in sorted right half + low = mid + 1; + } else { // lies in left half + high = mid - 1; + } + } + } + return -1; // not found + } +}; + +int main() { + Solution solution; + vector nums = {4,5,6,7,0,1,2}; + int index = solution.search(nums, 3); + if (index == -1) { + cout << "3 not found" << endl; + } else { + cout << "3 found at index " << index << endl; + } + index = solution.search(nums, 6); + if (index == -1) { + cout << "6 not found" << endl; + } else { + cout << "6 found at index " << index << endl; + } +} \ No newline at end of file diff --git a/problem3.cpp b/problem3.cpp new file mode 100644 index 00000000..e13bdc73 --- /dev/null +++ b/problem3.cpp @@ -0,0 +1,31 @@ +// Time Complexity : O(log n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Your code here along with comments explaining your approach in three sentences only +// First we run a while loop to find the bounds of the array by doubling the high pointer and to check which low-high range to perform binary search in. +// Next, we run a normal binary search loop to find the target using the reader.get() method. +// If we don't find the element, return -1. + +class Solution { +public: + int search(ArrayReader& reader, int target){ + int low = 0; + int high = 1; + while(reader.get(high) < target){ // to get the range for our binary search + low = high; + high = high * 2; // double the high + } + while(low <= high){ // standard binary search + int mid = low + (high - low)/2; + if(reader.get(mid) == target) return mid; + if(reader.get(mid) > target){ + high = mid - 1; + } else { + low = mid + 1; + } + } + return -1; // not found + } +}; \ No newline at end of file