by AskAI

Solutions:

Check these solutions from our community and artificial intelligence:
This solution is optimal because it is O(n) time complexity. However, it only works if the array is sorted. If the array is not sorted, this solution will not work.