Given an unsorted array arr[] and an element x, find floor and ceiling of x in arr[0..n-1].

**Floor** of x is the largest element which is smaller than or equal to x. Floor of x doesn’t exist if x is smaller than smallest element of arr[].

**Ceil** of x is the smallest element which is greater than or equal to x. Ceil of x doesn’t exist if x is greater than greates element of arr[].

**Examples:**
**Input :** arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
x = 7
**Output **: Floor = 6
Ceiling = 8
**Input :** arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
x = 10
**Output :** Floor = 9
Ceil doesn't exist.
**Input :** arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
x = 2
**Output :** Floor doesn't exist
Ceil = 5

**Example :
Input : **

The first line of input contains an integer T denoting the Test cases. Then T test cases follow.

First line contains no. of array elements - N and value of x

Second line contains array elements A[i]

**Output : **

Floor and Ceil Value of x

**Constraints :**

1 ≤ T ≤ 100

1 ≤ N, x ≤ 10^5

0 ≤ A[i] ≤ 10^6

**Input :**

2

8 2

5 6 9 8 6 5 5 6

11 264

147 154 383 223 345 30 376 111 33 186 72

**Output : **

Floor doesn't exist

5

223

345

Author: Mohit Gupta 4

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

mantan_g | 267 |

tushki | 198 |

aksht7 | 182 |

ayush1101 | 128 |

namansharmacse17 | 110 |

ARUN_PUNDIR | 730 |

manishgupta8497 | 627 |

AMAN__JAIN | 625 |

Abhimanyu_kumar | 562 |

AJ786 | 541 |

blackshadows | 5335 |

Ibrahim Nash | 5223 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4584 |

Login to report an issue on this page.