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
46 changes: 46 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Problem: Search in Rotated Sorted Array

// The approach uses a modified Binary Search.
// Even in a rotated sorted array, at least one half (left or right) of any 'mid' index will be sorted.
// 1. Identify which half is sorted.
// 2. Check if the target lies within the boundaries of that sorted half.
// 3. Move the search range accordingly.
//
// Time Complexity: O(log n) - We divide the search space by half in each step.
// Space Complexity: O(1) - We use only a few pointers.
class Solution {
public int search(int[] nums, int target) {
if(nums.length == 0){ // if array is empty, we don't need to check if target is present in it or not
return -1;
}
int low = 0;
int high = nums.length - 1;

while(low <= high){
int mid = low + (high - low)/2; // to avoid integer overflow

if(nums[mid] == target){ // check for target at mid
return mid;
}
// if array is left sorted
if(nums[low] <= nums[mid]){
if(nums[low] <= target && nums[mid] > target){ // check if target is in sorted range or not
high = mid - 1;
}else{
low = mid + 1;
}
}else{
// array is right sorted
if(nums[mid] < target && nums[high] >= target){ // check if target is in sorted range or not
low = mid + 1;
}else{
high = mid - 1;
}
}

}
// we didnt find the target
return -1;

}
}
45 changes: 45 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/

// Problem: Search in a Sorted Array of Unknown Size
// The approach uses two phases:
// 1. Range Finding: Since the size is unknown, we exponentially increase the 'high' pointer
// (doubling it) until we find a range [low, high] that could contain the target.
// 2. Binary Search: Once the range is identified, perform a standard binary search.
//
// Time Complexity: O(log n) where n is the index of the target element.
// The range finding takes O(log n) steps, and binary search takes O(log n).
// Space Complexity: O(1) as we only use pointers.

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

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

// Normal binary search to find the target
while(low<=high){
int mid = low + (high - low)/2 ; // to avoid interger overflow
if(reader.get(mid) == target){
return mid; // we found the target
}
if(reader.get(mid) > target){
high = mid - 1;
}else{
low = mid + 1;
}
}
// we didn't find the target
return -1;
}
}
77 changes: 77 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//Problem 3: Search a 2D Matrix

// 1st Approach:
// Considering matrix as imaginary 1D array and doing binary search on it
// This approach treats the matrix as a single sorted list.
// Mapping 1D index to 2D: row = index / columns, col = index % columns.
class Solution {
// Time Complexity: O(log(m) + log(n)) = O(log(mn))
//Space Complexity: O(1)
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;

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

while(low <= high){
int mid = low + (high - low)/2; // to avoid integer overflow
int r = mid / n;
int c = mid % n;
if(matrix[r][c] == target){
return true;
}
if(matrix[r][c] > target){
high = r * n + c - 1;
}else{
low = r * n + c + 1;
}
}

return false;
}
}

// 2nd approach:
// Doing binary search on row first to get the target's possible row. Then perform binary search on columns to search the target
// This is a two-step approach:
// 1. Find the potential row where the target could exist.
// 2. Perform a standard binary search within that specific row.
class Solution2 {
// Time Complexity: O(log(m) + log(n)) = O(log(mn))
//Space Complexity: O(1)
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int low = 0;
int high = m - 1;
int mid = 0;
// perform binary search on rows to get the range
while(low <= high){
mid = low + (high - low)/2;
if(matrix[mid][0] <= target && matrix[mid][n-1] >= target){
break;
}
else if(matrix[mid][0] > target && matrix[0][0] <= target){
high = mid - 1;
}else{
low = mid + 1;
}
}
low = 0;
high = n - 1;
int row = mid;
// perform second binary search to find the target
while(low <= high){
mid = low + (high - low)/2;
if(matrix[row][mid] == target){
return true;
}
else if(matrix[row][mid] > target){
high = mid - 1;
}else{
low = mid + 1;
}
}
return false;
}