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
3 changes: 3 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.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

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

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

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

43 changes: 43 additions & 0 deletions Matrix_Search_2D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Matrix_Search_2D {
//To search an element in log time, we'll use Binary search. For Binary search array should be sorted.
//Given 2D array in which last element of the row is smaller than the first element of the next row
//Time complexity of this algorithm will be O(log m*n), where m*n is the size of the array.
//Space complexity will be O(1), because we are storing in 2D array
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length==0){ //Base case to check if matrix is empty and return if it is empty
return false;
}
int m = matrix.length; //this will give the length of each row.
int n = matrix[0].length; //this will give the length of each column.
int low = 0; //lower bound of the array.
int high = m * n - 1; //upper bound of the array.


while(low <= high){
// int mid=(low+high)/2; //This will the index of middle element.
int mid= low+(high-low)/2; //other way to get the index of middle element to avoid the situtation of
// index overflow where the value goes out of the range.
int row = mid/n; // to get the row number of the element in 2D array, where n is the total no. of
//columns in 2D array
int col = mid%n; //to get the col number of the element in the 2D array, where n is the total number
// of columns in 2D array
if (matrix[row][col]==target){ //Condn to check if target is equal to middle element.
return true; //If yes then return true
}
else if(target> matrix[row][col]){ //Condn to check if target is greater than middle element.
low =mid+1; //If yes, then target must be on the right hand side of the array.Therefore, low will become mid+1.
}
else{ //If target is smaller than the middle element, this means element lies on th left side of the array and high will change to mid-1
high = mid-1;
}
}
return false; //If element is not found in the array, then return false
}

public static void main(String[] args){
Matrix_Search_2D m= new Matrix_Search_2D();
int[][] matrix= {{1,2,3},{10,11,16,20},{23,30,34,60}};
int target= 3;
System.out.println(m.searchMatrix(matrix,target));
}
}
49 changes: 49 additions & 0 deletions Rotated_Sorted_Array_Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Rotated_Sorted_Array_Search {
//1D Array is in asc order initially before rotation and have distinct elements.
//Assuming at least one side of the array will remain in sorted order after rotation.
//We can directly do linear search but that will not take O(log n) time. So we will do binary search.
//Time complexity is O(log n)
//Space complexity is O(n)
public int search(int[] nums, int target) {
if(nums==null ||nums.length==0){ //Condn to check if the array is empty
return -1; //If yes then return -1
}

int low=0; //lower bound of the array.
int high=nums.length-1; //upper bound of the array.


while(low<=high){
int mid = low +(high-low)/2; //we do high-low in order to avoid the interger overflow situation
if(nums[mid]==target){ //Condn to check if target is at middle index
return mid; //if yes, then return mid index as target is found
}

if(nums[low]<= nums[mid]){ //if target is not found at mid element and nums[low]<nums[mid], as one side is sorted we'll check on left side first.
if(target>= nums[low] && target <= nums[mid]){ //Cond to check if target lies between low and mid
high = mid-1; //then high will change to mid-1
}
else{
low=mid+1; //then it lies on the other side of the array.
}
}
else{ //If it is not on left side we'll check on right side of the array.
if(target>=nums[mid] && target<=nums[high]){
low=mid+1;
}
else{
high=mid-1;
}
}
}
return -1; //If not present in the array then return -1

}

public static void main(String[] args){
Rotated_Sorted_Array_Search rot_arr= new Rotated_Sorted_Array_Search();
int[] nums= {4,5,6,7,0,1,2};
int target= 7;
System.out.println(rot_arr.search(nums,target));
}
}
52 changes: 52 additions & 0 deletions SearchInfiniteArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//You have a sorted array of unique elements and an unknown size. You do not have an access to the array but you can use the ArrayReader interface to access it. You can call ArrayReader.get(i) that:
// returns the value at the ith index (0-indexed) of the secret array (i.e., secret[i]), or
// returns 231 - 1 if the i is out of the boundary of the array.
// You are also given an integer target.
//Return the index k of the hidden array where secret[k] == target or return -1 otherwise.
//You must write an algorithm with O(log n) runtime complexity.
// https://leetcode.ca/2017-11-01-702-Search-in-a-Sorted-Array-of-Unknown-Size/


//Sorted array of Infinite size with unique elements is given.Need to return the index where target is present.
//TC is O(log N)
//SC is O(1)

public class SearchInfiniteArray {

public int find(int[] arr, int target) {
int low = 0;
int high = 1; //As we don't have any high value so we'll initially take the adjacant value and keep on changing

while (arr[high] < target) { //This loop is to find out the high and low range so that we can apply binary search on this.
low=high;
high = high*2;
}

return binarySearchFxn(arr,target,high,low);

}

private int binarySearchFxn(int[] arr, int target, int low, int high){
while(low<=high){
int mid= low +(high-low)/2; //To prevent integer overflow
if (arr[mid] == target){
return mid;
}
else if (target>arr[mid]){
low=mid+1;
}
else{
high=mid-1;
}

}
return -1;
}

public static void main(String[] args){
SearchInfiniteArray obj= new SearchInfiniteArray();

int[] arr={1,2,3,4,7,8,12,16,24,45,67,88,99,100,101,102,103,104,105,106};
System.out.println(obj.find(arr,24));
}
}
3 changes: 3 additions & 0 deletions out/production/Binary-Search-1/.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 out/production/Binary-Search-1/.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 out/production/Binary-Search-1/.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 out/production/Binary-Search-1/.idea/modules.xml

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

Loading