Skip to content

Commit db4f4b1

Browse files
Atharva ThakurAtharva Thakur
authored andcommitted
Ceiling of a number in a list program added
1 parent 09f644f commit db4f4b1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cielingOfNumInList.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# In this problem we have to find a number equal to or the smallest greatest number to the given target number.
2+
3+
# so we use binary search for this
4+
# case 1: mid==target
5+
# so we have found a number equal to the target. problem solved
6+
# case 2 : element is not in the array
7+
# in this case we would reach the start>end and the while loop would stop.
8+
# and the next greater element would be pointed at by the start.
9+
# so the ceiling of the number would be arr[start].
10+
11+
arr =[1,2,3,4,8,9,10,15,19,23,27,29,34,39,45,46,55,59,60,63,68,69]
12+
target = int(input("Enter the target element->"))
13+
start = 0
14+
end = len(arr)-1
15+
16+
while(start<end):
17+
mid = int(start + (end - start) / 2)
18+
if target == arr[mid]:
19+
print(f"The ceiling of the element is {arr[mid]}")
20+
break
21+
elif target<arr[mid]:
22+
end=mid-1
23+
elif target>arr[mid]:
24+
start=mid+1
25+
else:
26+
print(f"The ceiling of the element is {arr[start]}")

0 commit comments

Comments
 (0)