문제
나의 풀이
1. 함수 만들어서.. 재귀함수로
import sys
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
def search(arr, target, start, end):
if start > end:
return -1
mid = (start + end) // 2
if arr[mid] = mid:
return mid
elif arr[mid] > mid:
return search(arr, target, start, mid - 1)
else:
return search(arr, target, mid + 1, end)
print(search(arr, x, 0, n))
2. 그냥 전형적인 이진탐색으로
n = int(input())
arr = list(map(int, input().split()))
start = 0
end = n - 1
for i in arr:
mid = (start + end) // 2
if mid == arr[mid]:
print(mid)
break
elif mid > arr[mid]:
start = mid + 1
else:
end = mid - 1
if end < start:
print(-1)
break