@@ -34,55 +34,6 @@ The recursive version simply expresses this idea as a function that keeps callin
34345 . Start the recursion with the full range ` [0, n - 1] ` .
35356 . Return the final result.
3636
37- <details >
38- <summary >Example - Dry Run</summary >
39-
40- Input: nums = [ -1, 0, 3, 5, 9, 12] , target = 9
41-
42- ** Initial Array:**
43-
44- ``` markdown
45- ┌────┬────┬────┬────┬────┬────┐
46- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
47- └────┴────┴────┴────┴────┴────┘
48- 0 1 2 3 4 5
49- ```
50-
51- ** Call 1:** binary_search(l=0, r=5)
52-
53- ``` markdown
54- L M R
55- ↓ ↓ ↓
56- ┌────┬────┬────┬────┬────┬────┐
57- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
58- └────┴────┴────┴────┴────┴────┘
59- 0 1 2 3 4 5
60-
61- l = 0, r = 5, m = 2
62- nums[ M] = nums[ 2] = 3
63- 3 < 9 (target)
64- → Search right half: binary_search(3, 5)
65- ```
66-
67- ** Call 2:** binary_search(l=3, r=5)
68-
69- ``` markdown
70- L M R
71- ↓ ↓ ↓
72-
73- ┌────┬────┬────┬────┬────┬────┐
74- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
75- └────┴────┴────┴────┴────┴────┘
76- 0 1 2 3 4 5
77-
78- l = 3, r = 5, m = 4
79- nums[ M] = nums[ 4] = 9
80- 9 == 9 (target) ✓ Found!
81- ```
82-
83- ** Result: index 4**
84-
85- </details >
8637
8738<br >
8839
@@ -300,53 +251,6 @@ We adjust the left and right pointers until we either find the target or the poi
300251 - If ` nums[m] > target ` , move search to the left half: update ` r = m - 1 ` .
3012523 . If the loop ends without finding the target, return ` -1 ` .
302253
303- <details >
304- <summary >Example - Dry Run</summary >
305-
306- Input: nums = [ -1, 0, 3, 5, 9, 12] , target = 9
307-
308- ** Initial Array:**
309-
310- ``` markdown
311- ┌────┬────┬────┬────┬────┬────┐
312- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
313- └────┴────┴────┴────┴────┴────┘
314- 0 1 2 3 4 5
315- ```
316-
317- ** Step 1:** L = 0, R = 5, M = 2
318-
319- ``` markdown
320- L M R
321- ↓ ↓ ↓
322- ┌────┬────┬────┬────┬────┬────┐
323- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
324- └────┴────┴────┴────┴────┴────┘
325- 0 1 2 3 4 5
326-
327- nums[ M] = nums[ 2] = 3
328- 3 < 9 (target)
329- → Search right half: L = M + 1 = 3
330- ```
331-
332- ** Step 2:** L = 3, R = 5, M = 4
333-
334- ``` markdown
335- L M R
336- ↓ ↓ ↓
337-
338- ┌────┬────┬────┬────┬────┬────┐
339- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
340- └────┴────┴────┴────┴────┴────┘
341- 0 1 2 3 4 5
342-
343- nums[ M] = nums[ 4] = 9
344- 9 == 9 (target) ✓ Found!
345- ```
346-
347- ** Result: index 4**
348-
349- </details >
350254
351255<br >
352256
@@ -569,84 +473,6 @@ Then we simply check whether the element just before that boundary is the target
5694734 . If ` l > 0 ` and ` nums[l - 1] == target ` , return ` l - 1 ` .
5704745 . Otherwise, return ` -1 ` (target not found).
571475
572- <details >
573- <summary >Example - Dry Run</summary >
574-
575- Input: nums = [ -1, 0, 3, 5, 9, 12] , target = 9
576-
577- The upper bound approach finds the first index where value > target, then checks index - 1.
578-
579- ** Initial Array:**
580-
581- ``` markdown
582- ┌────┬────┬────┬────┬────┬────┐
583- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
584- └────┴────┴────┴────┴────┴────┘
585- 0 1 2 3 4 5
586-
587- Note: R starts at index 6 (past end of array)
588- ```
589-
590- ** Step 1:** L = 0, R = 6, M = 3
591-
592- ``` markdown
593- L M R
594- ↓ ↓ ↓
595- ┌────┬────┬────┬────┬────┬────┐
596- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │ (6)
597- └────┴────┴────┴────┴────┴────┘
598- 0 1 2 3 4 5
599-
600- nums[ M] = nums[ 3] = 5
601- 5 <= 9 (target)
602- → Move left pointer: L = M + 1 = 4
603- ```
604-
605- ** Step 2:** L = 4, R = 6, M = 5
606-
607- ``` markdown
608- L M R
609- ↓ ↓ ↓
610-
611- ┌────┬────┬────┬────┬────┬────┐
612- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │ (6)
613- └────┴────┴────┴────┴────┴────┘
614- 0 1 2 3 4 5
615-
616- nums[ M] = nums[ 5] = 12
617- 12 > 9 (target)
618- → Move right pointer: R = M = 5
619- ```
620-
621- ** Step 3:** L = 4, R = 5, M = 4
622-
623- ``` markdown
624- L,M R
625- ↓ ↓
626-
627- ┌────┬────┬────┬────┬────┬────┐
628- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
629- └────┴────┴────┴────┴────┴────┘
630- 0 1 2 3 4 5
631-
632- nums[ M] = nums[ 4] = 9
633- 9 <= 9 (target)
634- → Move left pointer: L = M + 1 = 5
635- ```
636-
637- ** Final Check:**
638-
639- ``` markdown
640- L = 5 (upper bound: first index where value > target)
641- L - 1 = 4
642- nums[ 4] = 9 == 9 (target) ✓
643-
644- Return L - 1 = 4
645- ```
646-
647- ** Result: index 4**
648-
649- </details >
650476
651477<br >
652478
@@ -856,84 +682,6 @@ This approach is especially useful for sorted arrays because it avoids overshoot
8566824 . If ` l ` is within bounds and ` nums[l] == target ` , return ` l ` .
8576835 . Otherwise, return ` -1 ` (the target is not in the array).
858684
859- <details >
860- <summary >Example - Dry Run</summary >
861-
862- Input: nums = [ -1, 0, 3, 5, 9, 12] , target = 9
863-
864- The lower bound approach finds the first index where value >= target.
865-
866- ** Initial Array:**
867-
868- ``` markdown
869- ┌────┬────┬────┬────┬────┬────┐
870- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
871- └────┴────┴────┴────┴────┴────┘
872- 0 1 2 3 4 5
873-
874- Note: R starts at index 6 (past end of array)
875- ```
876-
877- ** Step 1:** L = 0, R = 6, M = 3
878-
879- ``` markdown
880- L M R
881- ↓ ↓ ↓
882- ┌────┬────┬────┬────┬────┬────┐
883- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │ (6)
884- └────┴────┴────┴────┴────┴────┘
885- 0 1 2 3 4 5
886-
887- nums[ M] = nums[ 3] = 5
888- 5 < 9 (target)
889- → Move left pointer: L = M + 1 = 4
890- ```
891-
892- ** Step 2:** L = 4, R = 6, M = 5
893-
894- ``` markdown
895- L M R
896- ↓ ↓ ↓
897-
898- ┌────┬────┬────┬────┬────┬────┐
899- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │ (6)
900- └────┴────┴────┴────┴────┴────┘
901- 0 1 2 3 4 5
902-
903- nums[ M] = nums[ 5] = 12
904- 12 >= 9 (target)
905- → Move right pointer: R = M = 5
906- ```
907-
908- ** Step 3:** L = 4, R = 5, M = 4
909-
910- ``` markdown
911- L,M R
912- ↓ ↓
913-
914- ┌────┬────┬────┬────┬────┬────┐
915- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
916- └────┴────┴────┴────┴────┴────┘
917- 0 1 2 3 4 5
918-
919- nums[ M] = nums[ 4] = 9
920- 9 >= 9 (target)
921- → Move right pointer: R = M = 4
922- ```
923-
924- ** Final Check:**
925-
926- ``` markdown
927- L = 4, R = 4 → Loop ends (L == R)
928- L = 4 is within bounds (< 6)
929- nums[ 4] = 9 == 9 (target) ✓
930-
931- Return L = 4
932- ```
933-
934- ** Result: index 4**
935-
936- </details >
937685
938686<br >
939687
@@ -1121,91 +869,6 @@ impl Solution {
1121869
1122870## 5. Built-In Function
1123871
1124- <details >
1125- <summary >Example - Dry Run</summary >
1126-
1127- Input: nums = [ -1, 0, 3, 5, 9, 12] , target = 9
1128-
1129- Built-in functions abstract the binary search logic. Here is how they work internally:
1130-
1131- ** Initial Array:**
1132-
1133- ``` markdown
1134- ┌────┬────┬────┬────┬────┬────┐
1135- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
1136- └────┴────┴────┴────┴────┴────┘
1137- 0 1 2 3 4 5
1138- ```
1139-
1140- ** Using Python's ` bisect_left ` (or similar):**
1141-
1142- The function finds the leftmost position where target can be inserted to maintain sorted order.
1143-
1144- ``` markdown
1145- ↓
1146- bisect_left
1147- returns 4
1148-
1149- ┌────┬────┬────┬────┬────┬────┐
1150- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
1151- └────┴────┴────┴────┴────┴────┘
1152- 0 1 2 3 4 5
1153- ```
1154-
1155- ** Internal Binary Search (what the built-in does):**
1156-
1157- Step 1: L = 0, R = 6, M = 3
1158-
1159- ``` markdown
1160- L M R
1161- ↓ ↓ ↓
1162- ┌────┬────┬────┬────┬────┬────┐
1163- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │ (6)
1164- └────┴────┴────┴────┴────┴────┘
1165- 0 1 2 3 4 5
1166- nums[ 3] = 5 < 9 → L = 4
1167- ```
1168-
1169- Step 2: L = 4, R = 6, M = 5
1170-
1171- ``` markdown
1172- L M R
1173- ↓ ↓ ↓
1174-
1175- ┌────┬────┬────┬────┬────┬────┐
1176- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │ (6)
1177- └────┴────┴────┴────┴────┴────┘
1178- 0 1 2 3 4 5
1179- nums[ 5] = 12 >= 9 → R = 5
1180- ```
1181-
1182- Step 3: L = 4, R = 5, M = 4
1183-
1184- ``` markdown
1185- L,M R
1186- ↓ ↓
1187-
1188- ┌────┬────┬────┬────┬────┬────┐
1189- │ -1 │ 0 │ 3 │ 5 │ 9 │ 12 │
1190- └────┴────┴────┴────┴────┴────┘
1191- 0 1 2 3 4 5
1192- nums[ 4] = 9 >= 9 → R = 4
1193- ```
1194-
1195- Loop ends: L = 4
1196-
1197- ** Verification:**
1198-
1199- ``` markdown
1200- index = 4
1201- nums[ 4] = 9 == 9 (target) ✓
1202-
1203- Return 4
1204- ```
1205-
1206- ** Result: index 4**
1207-
1208- </details >
1209872
1210873<br >
1211874
0 commit comments