-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindingfloor.py
More file actions
100 lines (52 loc) · 1.29 KB
/
findingfloor.py
File metadata and controls
100 lines (52 loc) · 1.29 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Finding the Floor
# Given an array, you have to find the floor of a number x. The floor of a number x is nothing but the largest number in the array less than or equal to x.
# Input Format
# The first line of input contains the N - the size of the array. The next line contains N integers, the elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains a single integer X, for which you have to find the floor of X in the given array.
# Output Format
# For each query, print the floor of X, separated by a new line. If the floor is not found, print the value of "INT_MIN".
# Constraints
# 30 points
# 1 <= N <= 105
# 1 <= Q <= 102
# -108 <= ar[i] <= 108
# 70 points
# 1 <= N <= 105
# 1 <= Q <= 105
# -108 <= ar[i] <= 108
# Example
# Input
# 6
# -6 10 -1 20 15 5
# 5
# -1
# 10
# 8
# -10
# -4
# Output
# -1
# 10
# 5
# -2147483648
# -6
# Explanation
# Self Explanatory
n=int(input())
ar=list(map(int,input().split()))
ar.sort()
T=int(input())
for i in range(T):
q=int(input())
f=-2147483648
l,h=0,n-1
while(l<=h):
mid=l+(h-l)//2
if(ar[mid]==q):
f=q
break
elif(ar[mid]>q):
h=mid-1
else:
f=ar[mid]
l=mid+1
print(f)