Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #find if the element is in the list
- #list ISN'T sorted
- n = int(input())
- a = list(map(int, input().split()))
- x = int(input())
- a.sort()
- #we consider elements in positions [l, r) -- a[l], a[l + 1], ..., a[r - 1].
- l = -1
- r = n
- while l < r - 1: #there is more than one element
- m = (l + r) // 2 #look at the value in the middle
- if a[m] < x:
- l = m
- else:
- r = m
- if r < n and a[r] == x: #if r ==n than there are no elements equal to x
- print('The leftmost occurrence is', r)
- else:
- print('There is no such element')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement