10 7
1 3 5 7 9 11 13 15 17 19
4
def binary_search(arr, target, start, end):
if start > end:
return None
mid = (start + end) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
end = mid - 1
elif arr[mid] < target:
start = mid + 1
return binary_search(arr, target, start, end)
n, target = map(int, input().split())
arr = list(map(int, input().split()))
res = binary_search(arr, target, 0, n - 1)
if res is None:
print(-1)
else:
print(res + 1)
from bisect import bisect_left, bisect_right
arr = [1, 2, 4, 4, 8]
target = 4
print(bisect_left(arr, target))
print(bisect_right(arr, target))
1 2 3 3 3 3 4 4 8 9
4
2
from bisect import bisect_left, bisect_right
def count_by_range(arr, left_val, right_val):
left_idx = bisect_left(arr, left_val)
right_idx = bisect_right(arr, right_val)
return right_idx - left_idx
arr = list(map(int, input().split()))
target = int(input())
res = count_by_range(arr, target, target)
print(res)
4 6
19 15 10 17
15
n, m = map(int, input().split())
arr = list(map(int, input().split()))
start = 0
end = max(arr)
res = 0
while start <= end:
mid = (start + end) // 2
total = 0
for a in arr:
if a > mid:
total += (a - mid)
if total >= m:
res = mid
start = mid + 1
else:
end = mid - 1
print(res)
7 2
1 1 2 2 2 2 3
4
from bisect import bisect_left, bisect_right
def count_by_range(arr, left_val, right_val):
left_idx = bisect_left(arr, left_val)
right_idx = bisect_right(arr, right_val)
return right_idx - left_idx
n, x = map(int, input().split())
arr = list(map(int, input().split()))
res = count_by_range(arr, x, x)
if res == 0:
print(-1)
else:
print(res)