Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5701 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 938 |

@bt8816103042 | 739 |

@SHOAIBVIJAPURE | 428 |

@codeantik | 412 |

@SherlockHolmes3 | 407 |

@neverevergiveup | 348 |

@mahlawatep | 347 |

@shalinibhataniya1097 | 343 |

@murarry3625 | 333 |

@saiujwal13083 | 326 |

@rohitanand | 314 |

Complete Leaderboard |

Given an unsorted array **Arr[]** of **N** integers and an integer **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 greatest element of **Arr[]**.

**Example 1:**

**Input:
**N = 8, X = 7
Arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
**Output:** 6 8
**Explanation:
**Floor of 7 is 6 and ceil of 7 is 8.

**Example 2:**

**Input:
**N = 8, X = 10
Arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
**Output:** 9 -1
**Explanation:
**Floor of 10 is 9 but ceil of 10 is not possible.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **getFloorAndCeil()** which takes the array of integers **arr, n** and **x **as parameters and returns a pair of integers denoting the answer. Return **-1** if floor or ceil is not present.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints :**

1 ≤ N ≤ 10^{5}

1 ≤ Arr[i], X ≤ 10^{6}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Ceil The Floor

...