-
Notifications
You must be signed in to change notification settings - Fork 0
/
BINARY_SEARCH.txt
42 lines (38 loc) · 1.09 KB
/
BINARY_SEARCH.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//------------------------------------------------------//
// //
// //
// SOURCE - https://www.youtube.com/watch?v=GU7DpgHINWQ //
// //
// //
//------------------------------------------------------//
1. Finding a target value in a sorted array
a <- sorted array
target <- represents the target value
PSEUDOCODE:
L = 0, R = N-1
while L <= R
mid = L + floor((R-L) / 2)
if a[mid] == target:
return mid
if a[mid] < target:
L = mid + 1
else:
R = mid = 1
return -1
2. First value >= target ( Lower bound of target )
PSEUDOCODE:
L = 0, R = N-1
ans = -1
while L <= R:
mid = L + floor((R - L) / 2)
if a[mid] >= target:
ans = a[mid]
R = mid-1
else:
L = mid+1
return ans
3. Rotated array: Somebody rotated( shifted ) a sroted array.
Find the smallest element
4. Find peak: The array increases and then decreases.
Find the maximum.
5. Sqrt(x) upto some precision