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
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Binary-Search-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ArrayReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface ArrayReader {
int get(int high);
}
44 changes: 44 additions & 0 deletions RotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Time Complexity: O(log(n))
Space Complexity : O(1)

Approach: Implementing binary search to find the value as we can reduce the search size by half with this approach
*/
public class RotatedSortedArray {
// Declare instance variables
private int low;
private int high;
private int mid;
public int search(int[] nums, int target) {
// Initialize the instance variables declared above
low =0 ;
high = nums.length-1;
mid = 0;

// Initialise the while loop with break condition low<=high to implement binary search
while(low <= high){
//Calculate mid value
mid = low + (high-low) / 2;
// If value at index mix is target return mid
if(nums[mid] == target) return mid;
// Reducing the search by half
else{
// If left side of mid is sorted
if(nums[low] <= nums[mid]){
// And target lies in left side
if(nums[low] <= target && nums[mid] >= target) high = mid-1;
// Remove right side
else low = mid+1;
}
// Right side is sorted
else{
//If target lies in right side remove left half
if(nums[mid] <= target && nums[high] >= target) low = mid+1;
// If not it should be left half so remove right half
else high = mid-1;
}
}
}
return -1;
}
}
7 changes: 0 additions & 7 deletions Sample

This file was deleted.

62 changes: 62 additions & 0 deletions SearchIn2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Time complexity: O(log(n)
Space Complexity: O(1)

Approach: We a 2 dimentional array with 2 conditions
1) Rows are sorted
2) At any give row last column value at previous row is less than the 1st column value of current row
If we convert the 2D array into a 1D array it looks like a sorted array of length ((m*n)-1) on which we
can apply binary search

To find the location this mid in 2 array we can use
row value = ((m*n)-1) / (length of column)
column value = ((m*n)-1) % (length of column)

Reason: Visualise it in 2D, we will have 1st row values followed 2nd row values etc... until length/(total number of columns)
So, to find exact row value we have to divide by column value

In the order of values in these rows will be in the order of columns so modulus of length will give the exact column location



*/
public class SearchIn2DMatrix {
// Declaring instance variables
private int low;
private int high;
private int mid;
private int rows;
private int columns;
public boolean searchMatrix(int[][] matrix, int target) {
// Intializing instance variables based on arguments
low = 0;
rows = matrix.length;
columns = matrix[0].length;
high = rows*columns -1;
// Initialise the while loop with break condition low<=high to implement binary search
while(low <= high){
// Calculating mid
mid = low + (high-low)/2;
// Based on mid calculating row and column values
int row = mid/columns;
int column = mid % columns;
// If target is equal to mid value return true
if(matrix[row][column] == target) return true;

// Reducing the search by half
else{
// If value lies in right half
if(matrix[row][column] < target){
// remove left half
low = mid+1;
}
else
{ // remove left half
high = mid -1;
}
}
}
// It the value isn't present in the array return false
return false;
}
}
44 changes: 44 additions & 0 deletions SearchInSortedArrayOfUnknownSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Time complexity: O(log(n))
Space complexity: O(1)

Approach:
Since we don't know the upper bound, we can start with index 0 and 1 and increase the search space by twice
until we exceed the upper limit.

Now do binary search on that limit

****Question I had:
Please correct me if my understanding is wrong***
Why twice: We can increase if by any value but it won't have any effect. It will for sure reduce number of steps in finding
the range but the search space in that range increases
*/
public class SearchInSortedArrayOfUnknownSize {
public int search(ArrayReader reader, int target) {
// Initialising the local variables
int low = 0;
int high =1;
int mid;

// While loop to find the search range
// Will stop if we find the range or if upper limit exceeds the length of the array.
while(target > reader.get(high)){
low = high;
high = high*2;
}

// Apply binary search on the range found above and do the search
while(low<= high){
mid = low + (high-low)/2;
if (reader.get(mid) == target) return mid;
if(reader.get(mid)>=target){
high = mid-1;
}
else{
low = mid+1;
}
}
// Return -1 if the value isn't present
return -1;
}
}