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
57 changes: 57 additions & 0 deletions SearchUnkownSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity :OLog(M)+OLog(N)
// Space Complexity :o(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :Got confused about the out of range condition, Need to think deeper on the question 2^31-1.


// Your code here along with comments explaining your approach in three sentences only
// I made use of the solution discussed in class.
// We are not given the size of the array.
// We can start with small values low=0 and high=1.
// To keep the N value low we have many conditions, we start high by 1, we increment it by 2*x and move low as well.
//Once range it fixed we follow the same binary search approach as we do in normal sorted array.
// We need to check the out of range condition as well, if the value is out of range we can return -1.

public class SearchUnkownSize {
/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/
interface ArrayReader {
public default int get(int index) {
return index;}
}

class Solution {
public int search(ArrayReader reader, int target) {

int low=0,high=1;
while(reader.get(high)<target)
{
low=high;
high=2*high;
}
while(low<=high)
{
int mid=low+(high-low)/2;

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


}
}
}

40 changes: 40 additions & 0 deletions SearchinMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity :O(Log(M*N))
// Space Complexity :o(1)
// Did this code successfully run on Leetcode :Yes
//Still confused about the index conversion, need to think deeper on the question.


// Your code here along with comments explaining your approach in three sentences only
// I made use of the solution discussed in class.
// We can treat the 2D matrix as a 1D array and apply binary search on it.
// We can calculate the mid index and convert it back to row
// and column index using the number of columns in the matrix.
// We can then compare the mid value with the target and adjust
// the low and high pointers accordingly until we find the target or the search space is empty.

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;

int left = 0;
int right = m * n - 1;

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

int row = mid / n;
int col = mid % n;

if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return false;
}
}
57 changes: 57 additions & 0 deletions SearchinRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

// 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
// I made use of the solution discussed in class.
//Linear Search is O(N)
//Atleast 1 half of the array is sorted,
// so we can check if the target lies in the sorted half and discard the other half.
// We repeat this process until we find the target or the search space is empty.
//We follow the binary search approach, but we need to determine which half of the array is sorted
// and whether the target lies in that half before deciding which half to search next.

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

//left side is sorted
while(low<=high)
{
int mid=low+(high-low)/2;
if(nums[mid]==target)
{
return mid;
}
if(nums[low]<=nums[mid])
{
if(nums[low]<=target && nums[mid]>target)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
//right side is sorted
else
{
if(target > nums[mid] && nums[high]>=target)
{
low=mid+1;
}
else
{
high=mid-1;
}
}

}
return -1;

}
}