forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_1.py
More file actions
47 lines (37 loc) · 1.28 KB
/
Exercise_1.py
File metadata and controls
47 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Time Complexity : O(log n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Your code here along with comments explaining your approach
# Approach:
# 1. Initialize two pointers, l (left) and r (right), to mark the search boundaries.
# 2. While l <= r:
# - Compute the middle index m = (l + r) // 2.
# - If nums[m] is greater than the target, move r to m - 1 (search left half).
# - If nums[m] is less than the target, move l to m + 1 (search right half).
# - If nums[m] equals the target, return m (element found).
# 3. If the loop ends without returning, the element is not present → return -1.
# Python code to implement iterative Binary
# Search.
# It returns location of x in given array arr
# if present, else returns -1
def binarySearch(arr, l, r, x):
l, r = 0, len(arr)-1
while l <= r:
m = (l + r) // 2
if arr[m] > x:
r = m - 1
elif arr[m] < x:
l = m + 1
else:
return m
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print ("Element is present at index % d" % result)
else:
print ("Element is not present in array")