Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions problem1.cpp
Original file line number Diff line number Diff line change
@@ -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 <vector>
#include <iostream>
using namespace std;

class Solution {
public:
bool searchMatrix(vector<vector<int>>& 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<vector<int>> 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.")<<endl;
cout<<(solution.searchMatrix(matrix, 35) ? "35 exists in the matrix." : "35 was not found in the matrix.")<<endl;
}
59 changes: 59 additions & 0 deletions problem2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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
// The core idea behind this problem is "atleast one half of a rotated sorted array will be sorted at any give point in the binary search".
// We start by finding the mid element and comparing with element at index low and high to determine which half is sorted.
// Then we check if the mid lies in that sorted half or not, based on this, we manipulate the low and high pointers.

#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
int search(vector<int>& 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<int> 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;
}
}
31 changes: 31 additions & 0 deletions problem3.cpp
Original file line number Diff line number Diff line change
@@ -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
}
};