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 Problem1.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


//Search in rotated sorted array
// - At each step, determines whether the left half or right half is sorted, then checks if the target lies within that sorted portion.
// - Narrows the search range accordingly and returns the target's index if found; otherwise returns -1.


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

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

if(nums[mid] == target) {
return mid;
}
if(nums[low] < nums[mid]) {
if(target >= nums[low] && target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if(target > nums[mid] && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}

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

// Search in a sorted array of unknown size
// - Finds a valid search range by exponentially expanding the upper bound until it is greater than or equal to the target
// - Performs binary search within the identified range to locate the target.

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

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

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

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

return -1;
}
}
38 changes: 38 additions & 0 deletions Problem3.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


//Search in 2D matrix
// - Treats the 2D matrix as a single sorted 1D array and applies binary search.
// - Converts the 1D middle index back to row and column coordinates using division and modulo operations.
// - Returns true if the target is found; otherwise returns false after the search space is exhausted.



class Problem1 {
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;
int low = 0, high = rows * cols - 1;

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

int midRow = mid / cols;
int midColumn = mid % cols;

if(matrix[midRow][midColumn] == target) {
return true;
}
if(target < matrix[midRow][midColumn]) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return false;
}
}