Skip to content

Binary-Search-1 Completed #2323

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
19 changes: 0 additions & 19 deletions README.md

This file was deleted.

7 changes: 0 additions & 7 deletions Sample

This file was deleted.

47 changes: 47 additions & 0 deletions search_2D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity :O(log((no.of rows)*(no.of columns))) == O(log(m*n))
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :if(l<=r) condition in binary search forgot to write


// Your code here along with comments explaining your approach in three sentences only

//doing binary search same as if these are arranged in an array . how ever without arranging in an array
//we can access the element and the index of the mid in the array
//finding row array index divided by n
//and column index is array index % n

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

class Solution {
bool binarysearch(vector<vector<int>>& matrix, int l, int h, int x){
while(l <= h){ // base condition

int mid = l+ ((h-l)/2);
int row = mid / matrix[0].size(); // finding row index
int column = mid % matrix[0].size(); // finding column index
if(matrix[row][column] == x){
return true;
}
if(matrix[row][column] > x){
h = mid - 1; //changing high to mid-1
}
if(matrix[row][column] < x){
l = mid + 1; //changing low to mid + 1
}
}
return false;

}
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty()){ // if matrix is empty base case
return false;
}
int rows = matrix.size();
int columns = matrix[0].size();
return binarysearch(matrix, 0, ((rows * columns)-1), target);
}
};
56 changes: 56 additions & 0 deletions search_rotatedarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Time Complexity :O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :if(l<=r) condition in binary search forgot to write
// and in left sorted checking arr[low]<= target && target <=arr[mid - 1] --not possible if only one element left
//we can just write like target < arr[mid];


// Your code here along with comments explaining your approach in three sentences only
// calculate min
//atleast oneside of min is sorted
//if arr[low] <= arr[mid] then left side is sorted go to left side and check if it is possible then do binary search
//if not possible then got to right side and check like this only

//if left side is not sorted then right side is sorted

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

class Solution {
private:
int binarysearch(vector<int>& nums, int low, int high, int target){
while(low <= high){ //checking condition
int mid = low + ((high-low)/2); // calculating mid
if(nums[mid] == target){ //if mid element is target return
return mid;
}
//atleast oneside is sorted
if(nums[low] <= nums[mid]){ //then left side is sorted
if(nums[low] <= target && target <= nums[mid]){// then do sorting in left side
high = mid -1;//O(logn) time complexity
}
else{// if not go to right side
low = mid + 1;
}
}
else{//right side is sorted
if(nums[mid+1] <= target && target <= nums[high]){ //do sorting on right side
low = mid + 1;
}
else{ //if not do it in left side
high = mid -1;
}

}
}
return -1; // as low became high target is not there

}
public:
int search(vector<int>& nums, int target) {
int size = nums.size(); //calculating size of array if size is 5 then high is 4
return binarysearch(nums, 0, size-1, target);
}
};