Floor and Ceil in Sorted Array
Learning 1D Array Binary Search DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Finding the floor and ceiling of a number in a sorted array using Binary Search, Binary Search, Arrays, Time Complexity, Space Complexity, iterative search, array properties, general programming, divide and conquer, binary search, time complexity analysis, Binary Search Variants, Iterative Search.
Floor and Ceiling in a Sorted Array Problem Statement You are given a sorted array arr of n integers and an integer x. Your task is to find the floor and ceiling of x in the array arr. The floor of x is defined as the largest element in the array that is less than or equal to x. The ceiling of x is defined as the smallest element in the array that is greater than or equal to x. Input Specification n: An integer representing the size of the array. arr[]: A sorted array of n integers. x: An integer for which to find the floor and ceiling. Output Specification Return two integers: the floor of x and the ceiling of x. If no floor or ceiling exists (e.g., all elements are greater than x for floor, or all elements are smaller than x for ceiling), return 1 for that value. Sample Test Cases Example 1: Example 2: