Implement binary search on a sorted integer array without using library functions. Provide both iterative and recursive versions that: (a) return the index of the target if present; (b) otherwise return the insertion index that would keep the array sorted. Clarify your loop invariants and boundary handling, support finding the first and last occurrence when duplicates exist, and prove correctness. State time and space complexity.