Skip to content

Commit b4cd832

Browse files
committed
First and last in sorted array
1 parent 539f96d commit b4cd832

3 files changed

Lines changed: 114 additions & 1 deletion

File tree

codes/find_first_last.mojo

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
### Find First/Last
2+
### Find first and last index of a target value in a sorted array
3+
4+
5+
fn find_first_last(arr: List[Int], target: Int) -> (Int, Int):
6+
result = (-1, -1)
7+
if len(arr) == 0:
8+
return result
9+
left, right = 0, len(arr) - 1
10+
11+
while left <= right:
12+
mid = (left + right) // 2
13+
if arr[mid] == target:
14+
result[1] = mid
15+
left = mid + 1
16+
elif arr[mid] > target:
17+
right = mid - 1
18+
else:
19+
left = mid + 1
20+
left, right = (
21+
0,
22+
result[1],
23+
) # result[1] -1 would keep left index at -1 for single occurence of target
24+
25+
while left <= right:
26+
mid = (left + right) // 2
27+
if arr[mid] == target:
28+
result[0] = mid
29+
right = mid - 1
30+
elif arr[mid] > target:
31+
right = mid - 1
32+
else:
33+
left = mid + 1
34+
return result
35+
36+
37+
from testing import assert_true
38+
39+
40+
fn main() raises:
41+
arr = List(5, 7, 7, 8, 8, 10)
42+
target = 8
43+
result = find_first_last(arr, target)
44+
assert_true(result[0] == 3 and result[1] == 4, "Assertion failed")
45+
target = 6
46+
result = find_first_last(arr, target)
47+
assert_true(result[0] == -1 and result[1] == -1, "Assertion failed")
48+
49+
arr = List(5, 7, 7, 8, 10)
50+
target = 8
51+
result = find_first_last(arr, target)
52+
assert_true(result[0] == 3 and result[1] == 3, "Assertion failed")

docs/find_first_last.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### Find First/Last
2+
### Find first and last index of a target value in a sorted array
3+
4+
```python
5+
6+
7+
fn find_first_last(arr: List[Int], target: Int) -> (Int, Int):
8+
result = (-1, -1)
9+
if len(arr) == 0:
10+
return result
11+
left, right = 0, len(arr) - 1
12+
13+
while left <= right:
14+
mid = (left + right) // 2
15+
if arr[mid] == target:
16+
result[1] = mid
17+
left = mid + 1
18+
elif arr[mid] > target:
19+
right = mid - 1
20+
else:
21+
left = mid + 1
22+
left, right = (
23+
0,
24+
result[1],
25+
) # result[1] -1 would keep left index at -1 for single occurence of target
26+
27+
while left <= right:
28+
mid = (left + right) // 2
29+
if arr[mid] == target:
30+
result[0] = mid
31+
right = mid - 1
32+
elif arr[mid] > target:
33+
right = mid - 1
34+
else:
35+
left = mid + 1
36+
return result
37+
38+
39+
from testing import assert_true
40+
41+
42+
fn main() raises:
43+
arr = List(5, 7, 7, 8, 8, 10)
44+
target = 8
45+
result = find_first_last(arr, target)
46+
assert_true(result[0] == 3 and result[1] == 4, "Assertion failed")
47+
target = 6
48+
result = find_first_last(arr, target)
49+
assert_true(result[0] == -1 and result[1] == -1, "Assertion failed")
50+
51+
arr = List(5, 7, 7, 8, 10)
52+
target = 8
53+
result = find_first_last(arr, target)
54+
assert_true(result[0] == 3 and result[1] == 3, "Assertion failed")
55+
56+
```
57+
58+
59+
[Source](https://github.com/ratulb/mojo_programming/blob/main/codes/find_first_last.mojo)

docs/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@
6565

6666
🟢 [Largest Number](largest_number.md) ➔ Arrange non-negative integers to form the largest possible number and return it as a string.
6767

68-
🟢 [Find All Anagrams in a String](all_anagrams.md) ➔ Return all start indices of anagrams of string p in string s.
68+
🟢 [Find All Anagrams in a String](all_anagrams.md) ➔ Return all start indices of anagrams of string p in string s.
69+
70+
🟢 [Find First/Last](find_first_last.md) ➔ Find first and last index of a target value in a sorted array

0 commit comments

Comments
 (0)