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
34 changes: 34 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 74. Search in a 2D Matrix
// Time Complexity : O(log(m * n))
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes (Could not try since LC premium Question but ran on multiple example values)
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach in three sentences only
// 1. Row 1 is sorted and the next row begans after first ends and is sorted again
// 2. Row 1,2,3 can be flattened into one big single row of length, m*n
// 3. Index of row can be calculated by i/n and columns as i%n where n would be the column.
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 midVal = matrix[mid / n][mid % n];

if (midVal == target) {
return true;
} else if (midVal < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}

return false;
}
}
44 changes: 44 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 33. Search in a Rotated Array

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

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

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

// Step 1: Identify which half is sorted
if (nums[low] <= nums[mid]) {
// Step 2: Check if target lies in left half
if (target >= nums[low] && target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else { // Right half is sorted
// Step 3: Check if target lies in right half
if (target > nums[mid] && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return -1;
}
}


// Time Complexity : O(Log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : I missed checking the value is pressent on the left side of mid and again checkin fthe righ side.


// Your code here along with comments explaining your approach in three sentences only
// Set Low and High to start and ending of the array. Figure the Mid
// Check which side is sorted from both, Low is less than mid then left side is shorter
// After Getting Shorter side choose if the target value lies on which side and do binary search on same.
36 changes: 36 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 702. Search in a Sorted Array of Unknown Size
import java.util.*;

class Problem3{
public int search(ArrayReader reader, int target){
int low=0;
int high=1;// To be able to multiply twice everytime
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(reader.get(mid) >target){
high=mid-1;
}
else{
low=mid+1;
}
}
return -1;// If Value is not Present
}
}

// Time Complexity : O(N)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes (Could not try since LC premium Question but ran on multiple example values)
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach in three sentences only
// 1. First I set the Low and HIgh pointer to 0 and 1 respectively and kep increasing the higher twice until its bigger than Mid.
// 2. After getting a range I could Apply the normal binary search