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
7 changes: 0 additions & 7 deletions Sample

This file was deleted.

36 changes: 36 additions & 0 deletions SearchElementInUnknownSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Time Complexity : O(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

// This code finds a target in an array of unknown size by first exponentially expanding the search range until the upper bound exceeds the target.
// Once the boundaries are established, it performs a standard Binary Search within that range to locate the specific index.

class SearchElementInUnknownSize {
public int search(ArrayReader reader, int target) {
int low = 0;
int 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)) {
low = mid + 1;
} else {
high = mid - 1;
}
}

return -1;
}
}
38 changes: 38 additions & 0 deletions SearchIn2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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

// This code performs a binary search on a 2D matrix by treating it as a single, sorted 1D array of length times n.
// It calculates the 2D coordinates using mid / n and mid % n to check the target, efficiently finding the value in O(log(m x n)) time.

public class SearchIn2DMatrix {
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;

}
}
38 changes: 38 additions & 0 deletions SearchInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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

// This implementation performs a Binary Search by first determining which half of the array is sorted relative to the midpoint.
// By comparing the target against the boundaries of that sorted half, it can reliably discard half of the search space in each step and finds the target.

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

while (left <= right) {
int mid = left + (right - left) / 2;
if (target == nums[mid]) return mid;
if (nums[left] <= nums[mid]) {
if (target >= nums[left] && 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;
}
}