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
29 changes: 29 additions & 0 deletions SearchIn2dMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time Complexity : O(log m*n) as it's binary search on the complete matrix size.
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Not much, once the idea to consider it as a single array and perform the binary search to achieve O(log m*n) it was
// better. The core part of the solution lies in identifying the position of mid element.

// Your code here along with comments explaining your approach


class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int low = 0;
int high = (matrix.length * matrix[0].length) - 1;
while(low <= high){
int mid = low + (high-low)/2;
int column = mid%matrix[0].length;
int row = mid/matrix[0].length;
if(matrix[row][column] == target){
return true;
}
if(matrix[row][column] > target){
high = mid - 1;
} else{
low = mid + 1;
}
}
return false;
}
}
30 changes: 30 additions & 0 deletions SearchInArrayReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity : O(log m) - m is the position of the target.
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Not much, once the idea to find the search place is cracked, it was a straight binary search on the space.

// Your code here along with comments explaining your approach


class Solution {
public int search(ArrayReader reader, int target) {
int low = 0, high = 1;

while(reader.get(high) < target){
low = high ;
high = high * 2;
}
//binary search
while(low <= high){
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;
}
}
38 changes: 38 additions & 0 deletions SearchInRotatedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Time Complexity : O(log n) as it's binary search.
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Not much, once the idea to find the sorted part on the array, it is a binary search to find the target.

// Your code here along with comments explaining your approach


class Solution {
public int search(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
int mid = 0;

while(low<=high){
mid = low + (high-low)/2; //to avoid number overflow
if(nums[mid] == target){
return mid;
}
if(nums[low] <= nums[mid]){ // considering the left half
if(nums[low] <= target && nums[mid] > target){ //checking if target is in left half
high = mid - 1;
}
else{
low = mid + 1;
}
}else{ // considering the right half
if(nums[mid] < target && nums[high] >= target){
low = mid + 1;
}else{
high = mid - 1;
}
}
}

return -1;
}
}