// 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.
import Foundation
func searchInsertPositionOfK(arr: [Int], k: Int) -> Int {
for i in 0..<arr.count {
// If k is found or needs to be inserted before arr[i]
if arr[i] >= k {
return i
}
}
// If k is greater than all elements, it should be inserted at the end
return arr.count
}
let arr1 = [1, 3, 5, 6, 7, 8]
let k1 = 5
print(searchInsertPositionOfK(arr: arr1, k: k1))
let arr2 = [1, 3, 5, 6, 7, 8]
let k2 = 2
print(searchInsertPositionOfK(arr: arr2, k: k2))
let arr3 = [1, 3, 5, 6, 7, 8]
let k3 = 9
print(searchInsertPositionOfK(arr: arr3, k: k3))
/*
run:
2
1
6
*/