Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Implement Lower Bound | Floor in a Sorted Array (GFG)
    Data Structures & Algorithms

    Implement Lower Bound | Floor in a Sorted Array (GFG)

    codeanddebugBy codeanddebug2 July 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Featured image of question to implement lower bound
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Given a sorted array arr[] and an integer x, find the index (0-based) of the largest element in arr[] that is less than or equal to x. This element is called the floor of x. If such an element does not exist, return -1.

    Note: In case of multiple occurrences of ceil of x, return the index of the last occurrence.

    Here’s the [Problem Link] to begin with.

    Examples

    Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 5
    Output: 1
    Explanation: Largest number less than or equal to 5 is 2, whose index is 1.
    Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 11
    Output: 4
    Explanation: Largest Number less than or equal to 11 is 10, whose indices are 3 and 4. The index of last occurrence is 4.
    Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 0
    Output: -1 Explanation: No element less than or equal to 0 is found. So, output is -1.

    Constraints:

    • 1 ≤ arr.size() ≤ 106
    • 1 ≤ arr[i] ≤ 106
    • 0 ≤ x ≤ arr[n-1]
    Contents:
     [show]
    • Intuition & Approach
    • Code Implementation
    • Code Explanation
    • Dry Run (arr = [1, 2, 8, 10, 11], k = 9)
    • Time & Space Complexity
    • Conclusion

    Intuition & Approach

    Binary search divides the search space in half repeatedly:

    1. Keep a pointer lb ( lower bound) to remember the best floor index seen so far – start with -1 (meaning “no floor yet”).
    2. While low ≤ high
      • Compute middle mid = (low + high) // 2.
      • Case 1:arr[mid] ≤ k
        • arr[mid] is a valid (and possibly better) floor → update lb = mid.
        • But there might be an even closer floor to the right, so move low = mid + 1.
      • Case 2:arr[mid] > k
        • Anything at mid or right is too big, so shift left with high = mid – 1.
    3. Loop stops when low crosses high; lb now holds the index of the largest element ≤ k, or -1 if none exist.

    This is just a small tweak of classic binary search that tracks the last “good” position seen.


    Code Implementation

    class Solution:
        def findFloor(self, arr, k):
            n = len(arr)
            lb = -1               # floor index; -1 means no floor yet
            low, high = 0, n - 1  # binary-search boundaries
    
            while low <= high:
                mid = (low + high) // 2        # middle index
    
                if arr[mid] <= k:
                    lb = mid                   # potential (better) floor
                    low = mid + 1              # look on the right side
                else:
                    high = mid - 1             # look on the left side
    
            return lb                           # final floor index (or -1)
    Python

    Code Explanation

    • lb (lower bound) keeps the best floor found so far.
    • Binary search window [low … high] always contains candidates we haven’t ruled out.
    • When arr[mid] is ≤ k, it’s a valid floor, so store its index and push low right to hunt for an even closer floor.
    • When arr[mid] is > k, everything right of mid is larger than k; discard that half by moving high left.
    • The loop ends when no unexamined indices remain. The answer is whatever index lb holds (or -1).

    Dry Run (arr = [1, 2, 8, 10, 11], k = 9)

    Steplowhighmidarr[mid]lb (floor idx)Action
    1042828 ≤ 9 → lb = 2, low = 3
    234310210 > 9 → high = 2
    End————2loop stops (low > high)

    Output index 2 (value 8), which is the largest number ≤ 9.


    Time & Space Complexity

    MetricPreciseSimplified
    TimeO(log₂ n) comparisonsO(log n)
    SpaceO(1) auxiliaryConstant

    Binary search guarantees logarithmic time, and only a couple of integer variables are used, so space stays constant.


    Conclusion

    Finding the floor in a sorted array is a classic example of how small tweaks to binary search can solve seemingly new problems:

    • Track the best candidate while you divide the search space.
    • Update the candidate whenever you find a closer match.

    Master this pattern and you’ll be equipped for a wide range of “closest element” questions on sorted data. Happy searching!

    Join our Advance DSA COURSE

    For any changes to the article, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Array Binary Search
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleBinary Search | Leetcode 704 | Explained in Python
    Next Article Implement Upper Bound
    codeanddebug
    • Website

    Related Posts

    Data Structures & Algorithms

    Search in Rotated Sorted Array | Leetcode 33 | Optimal Binary Search

    3 July 2025
    Data Structures & Algorithms

    Count occurrences of a number in a sorted array with duplicates | Optimal Binary Search

    3 July 2025
    Data Structures & Algorithms

    Find First and Last Position of Element in Sorted Array | Leetcode 34 | Optimal Binary Search

    2 July 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (93)
      • Beginner (44)
      • Expert (22)
      • Intermediate (27)
    • Uncategorised (1)
    Recent Posts

    Search in Rotated Sorted Array | Leetcode 33 | Optimal Binary Search

    3 July 2025

    Count occurrences of a number in a sorted array with duplicates | Optimal Binary Search

    3 July 2025

    Find First and Last Position of Element in Sorted Array | Leetcode 34 | Optimal Binary Search

    2 July 2025

    Ceil The Floor | Binary Search Implementation

    2 July 2025

    Search Insert Position | Leetcode 35 | Explained in Python

    2 July 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.