fn find_first_and_last_position(arr: &[i32], n: i32) -> (Option<usize>, Option<usize>) {
let mut first = None;
let mut last = None;
for (i, &val) in arr.iter().enumerate() {
if val == n {
if first.is_none() {
first = Some(i);
}
last = Some(i);
}
}
(first, last)
}
fn main() {
let arr = [1, 3, 3, 3, 3, 1, 9];
let n = 3;
let (first, last) = find_first_and_last_position(&arr, n);
match (first, last) {
(Some(f), Some(l)) => println!("First position = {}, Last position = {}", f, l),
_ => println!("Not found"),
}
}
/*
run:
First position = 1, Last position = 4
*/