// Given a sorted array/vector/list of distinct integers and a target value K,
// return the index if the target is found.
// If not, return the index where it would be if it were inserted in order.
fn search_insert_position_of_k(vec: &[i32], k: i32) -> usize {
for (i, &val) in vec.iter().enumerate() {
// If k is found or needs to be inserted before val
if val >= k {
return i;
}
}
// If k is greater than all elements, it should be inserted at the end
vec.len()
}
fn main() {
let vec1 = vec![1, 3, 5, 6, 7, 8];
let k1 = 5;
println!("{}", search_insert_position_of_k(&vec1, k1));
let vec2 = vec![1, 3, 5, 6, 7, 8];
let k2 = 2;
println!("{}", search_insert_position_of_k(&vec2, k2));
let vec3 = vec![1, 3, 5, 6, 7, 8];
let k3 = 9;
println!("{}", search_insert_position_of_k(&vec3, k3));
}
/*
run:
2
1
6
*/