Skip to content

Completed binary search1 #2338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
29 changes: 29 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
43 changes: 43 additions & 0 deletions Problem1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time Complexity : O(log(m * 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
// Problem1 Search a 2D Matrix(https://leetcode.com/problems/search-a-2d-matrix/)

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

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size();
if (m == 0) return false;
int n = matrix[0].size();

//get start and end of the matrix
int start = 0;
int end = m * n - 1;

//for binary search get mid value
while (start <= end) {
int mid = start + (end - start) / 2;
int midVal = matrix[mid / n][mid % n];

//check if midval is equal to target or else, compare it to decide which side of matrix to move
if (midVal == target) {
return true;
} else if (target < midVal) {
end = mid - 1;
} else {
start = mid + 1;
}
}
// if target isnt found return false
return false;
}
};

45 changes: 45 additions & 0 deletions problem2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
int search(vector<int>& nums, int target) {
int start = 0;
int end = nums.size() - 1;

//to perform binary search on one part of the array. in a rotated sorted array one half is sorted
while (start <= end) {
int mid = start + (end - start) / 2;

if (nums[mid] == target)
return mid;

// Check if left half is sorted
if (nums[start] <= nums[mid]) {
if (nums[start] <= target && target < nums[mid])
end = mid - 1;
else
start = mid + 1;
}
// if Right half is sorted
else {
if (nums[mid] < target && target <= nums[end])
start = mid + 1;
else
end = mid - 1;
}
}
// if target isnt found
return -1;
}
};
48 changes: 48 additions & 0 deletions problem3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Time Complexity : O(log k)
// 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

//Search in Infinite sorted array:

//https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/

//Given a sorted array of unknown length and a number to search for, return the index of the number in the array. Accessing an element out of bounds throws exception. If the number occurs multiple times, return the index of any occurrence. If it isn’t present, return -1.

#include <climits>

class ArrayReader {
public:
int get(int index);
};

class Solution {
public:
int search(const ArrayReader& reader, int target) {
int start = 0, end = 1;

while(reader.get(end) < target) {
start = end;
end *=2;
}

while(reader.get(end) < target) {
start = end;
end *= 2;
}
while (start<=end) {
int mid = start + (end - start)/2;
int midVal = reader.get(mid);

if (midVal == target) return mid;
if (midVal > target || midVal == INT_MAX)
end = mid -1;
else
start = mid +1;
}
return -1;
}
};