Skip to content

Commit

Permalink
binary search variants
Browse files Browse the repository at this point in the history
  • Loading branch information
elarabyelaidy19 committed May 20, 2022
1 parent 385fcbd commit 3248cec
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
59 changes: 55 additions & 4 deletions BinarySearch/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ public class BinarySearch {
int peakIndexMountainArray(int[] arr) {
int l = 0;
int r = arr.length - 1;
int m = 0;
while (r > l) {
m = (l + r) / 2;
if (arr[m] < arr[m + 1])
int mid = (l + r) / 2;
if (arr[m] < arr[m + 1]) // until element < elment + 1 shrink, when condition fail we found Peak
l = m + 1;
else
r = m;
Expand Down Expand Up @@ -124,7 +123,7 @@ public int search(int[] nums, int target) {
if (nums[minIdx] == target)
return minIdx;
int n = nums.length - 1;
int lo = (nums[n] >= target) ? minIdx : 0; // where to go with lo [[4,5,6,7] [0,1,2]] 1 => lo = minIdx , hi = n
int lo = (nums[n] >= target) ? minIdx : 0; // where to go with lo [[4,5,6,7] [0,1,2]] t = 1 => lo = minIdx , hi = n
int hi = (nums[n] < target) ? minIdx : n; // where to go with hi

while (hi >= lo) {
Expand Down Expand Up @@ -212,5 +211,57 @@ public int findMinIndex(int[] nums) {
}
}
return res;
}


// ========================================================================================
// Given an array of integers nums sorted in non-decreasing order, find the
// starting and ending position of a given target value. If targe is not found in the array,return[-1,-1].

public int[] searchRange(int[] nums, int target) {
int[] res = { -1, -1 };
if (nums == null || nums.length == 0) {
return res;
}
res[0] = lower(nums, target);
res[1] = upper(nums, target);
return res;
}

public int lower(int[] nums, int t) {
int lo = 0;
int hi = nums.length - 1;
int lowr = -1;
while (hi >= lo) {
int mid = (hi + lo) >>> 1;
if (nums[mid] == t) {
lowr = mid; // set lowr = mid and goes left for another value with minimum idx [5,7,7,8,8,8,10] t =8
hi = mid - 1;
} else if (nums[mid] > t) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return lowr;
}

public int upper(int[] nums, int t) {
int lo = 0;
int hi = nums.length - 1;
int uppr = -1;
while (hi >= lo) {
int mid = (hi + lo) >>> 1;
if (nums[mid] == t) {
uppr = mid; // set upper = mid and goes right seacrh for another value with maximum idx
lo = mid + 1;
} else if (nums[mid] > t) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return uppr;
}

}
9 changes: 9 additions & 0 deletions BinarySearch/BinarySearch.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Missing
return for find K closest element.
complete explore card of Leetcode.


## [PeakMountainIndexArray](https://leetcode.com/problems/peak-index-in-a-mountain-array/)
- search for the first number that is greater than its neighbors. arr[i] > arr[i+1]
Expand All @@ -24,3 +28,8 @@
## [find minimum in roted sorted array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
- compare mid with high, if mid > high go right, if mid < high go left.
- templete 2





45 changes: 45 additions & 0 deletions BinarySearch/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,50 @@ used to search for an element or condition which requires accessing the current
- Searching Right: left = mid


```java

public int bsearch(int[] nums, int k) {
int lo = 0;
int hi = nums.length - 1;
while(lo + 1 < hi) {
int mid = (hi + lo) >>> 1;
if(nums[mid] == k) {
return mid;
} else if(nums[mid] < k) {
lo = mid;
} else {
hi = mid;
}
}
if(nums[lo] == k) return lo;
if(nums[hi] == k) return hi;
return -1;
}
```


# Notes
- when find minimum or maximum, upper bound and lower bound are needed. or range.

```java
public int findMinIndex(int[] nums) {
int lo = 0;
int hi = nums.length - 1;
int res = -1;
while(hi >= lo) {
int mid = (hi + lo) >> 1;
if(nums[mid] == mid) {
res = mid;
hi = mid - 1; // search in left for minimum element if there that satisfy
} else if(nums[mid] > mid) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return res;
}
```

# [Anlysis for the three templets](https://leetcode.com/explore/learn/card/binary-search/136/template-analysis/935/)
![differnces](https://leetcode.com/explore/learn/card/binary-search/136/template-analysis/Figures/binary_search/Template_Diagram.png)

0 comments on commit 3248cec

Please sign in to comment.