Skip to content

Commit 5ccad83

Browse files
Ceil in a Sorted Array
1 parent b12053e commit 5ccad83

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

Leetcode/Ceil_in_a_Sorted_Array.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
"""
2+
Problem: Ceil in a Sorted Array
3+
4+
Given a sorted array nums[] and an integer x,
5+
find the index (0-based) of the smallest element
6+
that is greater than or equal to x.
7+
8+
This element is called the ceil of x.
9+
10+
If such an element does not exist, return -1.
11+
12+
Examples:
13+
Input:
14+
nums = [1, 2, 8, 10, 11, 12, 19]
15+
x = 5
16+
17+
Output:
18+
2
19+
20+
Explanation:
21+
The smallest element >= 5 is 8,
22+
which is present at index 2.
23+
24+
--------------------------------------------------
25+
Approach: Binary Search
26+
--------------------------------------------------
27+
28+
Since the array is sorted, Binary Search can be used.
29+
30+
Observation:
31+
32+
1. If nums[mid] >= x:
33+
- nums[mid] can be a possible ceil.
34+
- Store mid in ans.
35+
- Search on the left side to find a smaller value
36+
that is still >= x.
37+
38+
2. If nums[mid] < x:
39+
- Current element cannot be the ceil.
40+
- Search on the right side.
41+
42+
The last valid stored index will be the answer.
43+
44+
--------------------------------------------------
45+
Dry Run
46+
--------------------------------------------------
47+
48+
nums = [1, 2, 8, 10, 11, 12, 19]
49+
x = 5
50+
51+
Initial:
52+
l = 0
53+
r = 6
54+
ans = -1
55+
56+
Iteration 1:
57+
mid = 3
58+
nums[mid] = 10
59+
60+
10 >= 5
61+
62+
Possible ceil found
63+
ans = 3
64+
65+
Search left side
66+
67+
r = 2
68+
69+
----------------------------------
70+
71+
Iteration 2:
72+
l = 0
73+
r = 2
74+
75+
mid = 1
76+
nums[mid] = 2
77+
78+
2 < 5
79+
80+
Move right
81+
82+
l = 2
83+
84+
----------------------------------
85+
86+
Iteration 3:
87+
l = 2
88+
r = 2
89+
90+
mid = 2
91+
nums[mid] = 8
92+
93+
8 >= 5
94+
95+
Possible ceil found
96+
ans = 2
97+
98+
Search left side
99+
100+
r = 1
101+
102+
Loop Ends
103+
104+
Answer = 2
105+
106+
--------------------------------------------------
107+
Time Complexity
108+
--------------------------------------------------
109+
110+
Binary Search reduces the search space by half
111+
in every iteration.
112+
113+
TC = O(log n)
114+
115+
--------------------------------------------------
116+
Space Complexity
117+
--------------------------------------------------
118+
119+
Only a few variables are used.
120+
121+
SC = O(1)
122+
123+
--------------------------------------------------
124+
"""
125+
126+
127+
class Solution:
128+
def findCeil(self, nums, x):
129+
"""
130+
Returns the index of the smallest element
131+
greater than or equal to x.
132+
133+
Parameters:
134+
nums (List[int]): Sorted array
135+
x (int): Target value
136+
137+
Returns:
138+
int: Index of ceil element, or -1 if not found
139+
"""
140+
141+
ans = -1
142+
143+
l = 0
144+
r = len(nums) - 1
145+
146+
while l <= r:
147+
mid = l + (r - l) // 2
148+
149+
if nums[mid] >= x:
150+
# Possible ceil found
151+
ans = mid
152+
153+
# Search for a smaller valid ceil
154+
r = mid - 1
155+
156+
else:
157+
# Current element is too small
158+
l = mid + 1
159+
160+
return ans
161+
162+
163+
# --------------------------------------------------
164+
# Example Usage
165+
# --------------------------------------------------
166+
167+
if __name__ == "__main__":
168+
169+
nums = [1, 2, 8, 10, 11, 12, 19]
170+
x = 5
171+
172+
sol = Solution()
173+
174+
result = sol.findCeil(nums, x)
175+
176+
print("Ceil Index:", result)
177+
178+
if result != -1:
179+
print("Ceil Value:", nums[result])
180+
else:
181+
print("No ceil exists.")

0 commit comments

Comments
 (0)