밑의 글은 이론적이고 복잡한 내용이고, 이건 바로 코테에 적용하기 좋은 코드
from bisect import bisect_left, bisect_right
arr = [1,2,4,4,8]
x = 4
# 해당 값 왼쪽 인덱스 반환
bisect_left(arr, x)
# 해당 값 오른쪽 인덱스 반환
bisect_right(arr, x)
# 두 수 사이의 개수 구하는 함수
def count_by_range(array, left, right):
left_index = bisect_left(array, left)
right_index = bisect_right(array, right)
return right_index - left_index
def bs(array, target, start, end):
while start <= end:
mid = (start + end) // 2
if array[mid] == target:
return mid
elif array[mid] > target:
end = mid - 1
else:
start = mid + 1
return None
n, target = list(map(int, input().split()))
array = list(map(int, input().split()))
result = bs(array, target, 0, n-1)
if result == None:
print("원소 없음")
else:
print(result + 1)
def bs(array, target, start, end):
if start > end:
return None
mid = (start + end) // 2
if array[mid] == target:
return mid
elif array[mid] > target:
return bs(array, target, start, mid-1)
else:
return bs(array, target, mid+1, end)
n, target = list(map(int, input().split()))
array = list(map(int, input().split()))
result = bs(array, target, 0, n-1)
if result == None:
print("원소 없음")
else:
print(result + 1)