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
36 changes: 36 additions & 0 deletions 2DMatrixSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Time Complexity : O(log m*n) where m is the number of rows and n is the number of columns in the matrix
// Space Complexity : O(1) as we are using constant space
// 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
//treat 2d matrix as 1d array and apply binary search to get row and column index from mid value and compare with target tp get the result.

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
Comment on lines +11 to +13

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;

}
}
53 changes: 53 additions & 0 deletions InfinitSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Time Complexity : O(log n) where n is the index of the target element
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : how to implement arrayreader

// Your code here along with comments explaining your approach in three sentences only
//Double the high index until the target is within range. then Perform binary search between low and high.


public 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;
if (reader.get(mid) > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
Comment on lines +14 to +27

return -1;
}

public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 12, 15, 20};
ArrayReader reader = new ArrayReader(arr);
int target = 9;

Solution sol = new Solution();
int idx = sol.search(reader, target);
System.out.println(idx);
}

static class ArrayReader {
private final int[] data;

public ArrayReader(int[] arr) {
this.data = arr == null ? new int[0] : arr.clone();
}
public int get(int index) {
if (index < 0 || index >= data.length) return Integer.MAX_VALUE;
return data[index];
}
}
}
51 changes: 51 additions & 0 deletions RotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity : O(log n) where n is the number of elements in the array
// 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
// Perform binary search on a rotated sorted array.Check if the left or right half is sorted and narrow search accordingly. Repeat until the target is found or search space is exhausted.
public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}

int left = 0;
int right = nums.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (nums[mid] == target) {
return mid;
}

// Identify which half is sorted
if (nums[left] <= nums[mid]) {
// Left half is sorted
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1; // Target is in the left sorted portion
} else {
left = mid + 1; // Target is in the right portion
}
} else {
// Right half is sorted
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1; // Target is in the right sorted portion
} else {
right = mid - 1; // Target is in the left portion
}
}
}

return -1;
}

public static void main(String[] args) {
Solution sol = new Solution();
int[] nums = {4, 5, 6, 7, 0, 1, 2};
System.out.println("Index of 0: " + sol.search(nums, 0));
System.out.println("Index of 3: " + sol.search(nums, 3));
}
}
Binary file added Solution$ArrayReader.class
Binary file not shown.
Binary file added Solution.class
Binary file not shown.