Menu Close

Write a efficient functions to find floor of x.

Given a sorted array and a value x, the floor of x is the largest element in array smaller than or equal to x. Write a efficient functions to find floor of x.

Input: arr[]={10,12,19,25,30}, x = 20
Output: 19

arr=list(map(int,input('Enter the array : ').split(' ')))
x=int(input('Enter the value of x : '))
temp=sorted(arr,reverse=True)
for i in range(len(arr)):
    if temp[i] <= x:
        print(temp[i])
        break

Input_1:
Enter the array : 10 12 19 25 30
Enter the value of x : 20
Output:
19


Input_2:
Enter the array : 10 20 30 40 50 60
Enter the value of x : 35
Output:
30


Input_3:
Enter the array : 1000 2000 3000
Enter the value of x : 2445
Output:
2000


Morae Q!