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
39 changes: 39 additions & 0 deletions SearchInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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
/*
we want to check and compare which sub array might have the target element,in this problem, given this
is sorted at a pivot element, we want to understand at which part did sorting happen, so, we try to check
the left/right direction by comparing left and mid or mid and right to get idea of the sorted range and
then have respective comparisons with target to split and continue the iteration
*/

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

while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] == target)
return mid;
else if(nums[left] <= nums[mid]) {
if(nums[left] <= target && target < nums[mid])
right = mid - 1;
else
left = mid + 1;
}
else {
if(target > nums[mid] && target <= nums[right])
left = mid + 1;
else
right = mid - 1;
}

}
return -1;
}
}
36 changes: 36 additions & 0 deletions Searcha2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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
/*
We use binary search as an optimal approach where we try to assume entire matrix as a single array and
eliminate low/high subarray search by checking the respective row and column index value comparisons.
During this process of viewing the matrix as single array, we iterate r and c values by divide and modulo
operations with the column and mid values.
*/
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;

int low = 0 , high = m*n-1;

while(low <= high) {
int mid = low + (high - low) / 2;

int r = mid / n;
int c = mid % n;

if(matrix[r][c] == target)
return true;
else if(matrix[r][c] > target)
high = mid - 1;
else
low = mid + 1;
}
return false;
}
}
42 changes: 42 additions & 0 deletions SearchinaSortedArrayofUnknownSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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
/*
We need to find the range of low and high by iteratively comparing the array's high value to target through
reader API call by starting at small values set to low and high and then slowly multipliying the high
by 2 because we want to maintain the range find logic and search logic later in a balanced way at log n
complexity. Once we find the range, we do regular binary search to check the target and return its index.
*/
/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/

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

while(reader.get(high) < target) {
low = high;
high = high * 2;
}

while(low <= high) {
int mid = low + (high - low) / 2;
if(reader.get(mid) == target)
return mid;
else if(target < reader.get(mid))
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
}