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.

makeitdonesolo | 64 |

ShubhamJain18 | 64 |

Bishnu Dev Panda | 54 |

DanishBansal | 50 |

abhithakur588 | 50 |

Bishnu Dev Panda | 777 |

yk12 | 512 |

okayboss | 490 |

DevMittal | 472 |

VijethaShastri | 431 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.