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
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Binary-Search-1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
44 changes: 44 additions & 0 deletions BinarySearch1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Search a 2D Matrix
// Time Complexity : O(log (r*c))
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
//

// Your code here along with comments explaining your approach in three sentences only

//Approach
// 1) Get rows from size of matrix and columns from each row of matrix
// 2) Binary search on indexes
// 3) convert into row and column

public class BinarySearch1 {
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length;
int cols = matrix[0].length;

int left = 0;
int right = rows * cols - 1;

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

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

int value = matrix[row][col];

if (value == target) {
return true;
}
else if (value < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}

return false;
}
}
42 changes: 42 additions & 0 deletions BinarySearch2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 33. Search in Rotated Sorted Array
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
//

// Your code here along with comments explaining your approach in three sentences only

//Approach
// 1) find the mid element
// 2) Sort the left oarray of the mid element
// 3) Sort the right array of mid element
// 4) return if found the target

public class BinarySearch2 {
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;

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

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

// Left half sorted
if (nums[left] <= nums[mid]) {
if (target >= nums[left] && target < nums[mid])
right = mid - 1;
else
left = mid + 1;
}
// Right half sorted
else {
if (target > nums[mid] && target <= nums[right])
left = mid + 1;
else
right = mid - 1;
}
}
return -1;
}
}
40 changes: 40 additions & 0 deletions BinarySearch3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 702. Search in a Sorted Array of Unknown Size
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :
//

// Your code here along with comments explaining your approach in three sentences only

//Approach
// 1) no fixed length given in this
// 2) find the range of search
// 3) Search using binary searchx
// 4) return if found the target

public class BinarySearch3 {
ppublic int search(ArrayReader reader, int target) {
int left = 0;
int right = 1;

while (reader.get(right) < target) {
left = right;
right = right * 2;
}

while (left <= right) {
int mid = left + (right - left) / 2;
int val = reader.get(mid);

if (val == target)
return mid;
else if (val < target)
left = mid + 1;
else
right = mid - 1;
}

return -1;
}
}
7 changes: 0 additions & 7 deletions Sample

This file was deleted.