Skip to content

Commit b12053e

Browse files
Floor in a Sorted Array
1 parent 6ea49d3 commit b12053e

1 file changed

Lines changed: 174 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)