{"id":503,"date":"2025-07-02T18:55:32","date_gmt":"2025-07-02T13:25:32","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=503"},"modified":"2025-07-02T18:55:34","modified_gmt":"2025-07-02T13:25:34","slug":"ceil-the-floor","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/","title":{"rendered":"Ceil The Floor | Binary Search Implementation"},"content":{"rendered":"\n<p>For a <strong>sorted<\/strong> array <code>a[]<\/code> of length <code>n<\/code> and a value <code>x<\/code>, you must return both:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Floor<\/strong> \u2013 the greatest element \u2264 <code>x<\/code><\/li>\n\n\n\n<li><strong>Ceil<\/strong> \u2013 the smallest element \u2265 <code>x<\/code><\/li>\n<\/ul>\n\n\n\n<p>If either does not exist, output <code>-1<\/code> in its place.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.naukri.com\/code360\/problems\/ceiling-in-a-sorted-array_1825401\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><code>a<\/code> (sorted)<\/th><th><code>x<\/code><\/th><th>Floor<\/th><th>Ceil<\/th><\/tr><\/thead><tbody><tr><td><code>[1, 2, 8, 10, 11, 12, 19]<\/code><\/td><td><code>0<\/code><\/td><td><code>-1<\/code><\/td><td><code>1<\/code><\/td><\/tr><tr><td><code>[1, 2, 8, 10, 11, 12, 19]<\/code><\/td><td><code>5<\/code><\/td><td><code>2<\/code><\/td><td><code>8<\/code><\/td><\/tr><tr><td><code>[1, 2, 8, 10, 11, 12, 19]<\/code><\/td><td><code>19<\/code><\/td><td><code>19<\/code><\/td><td><code>19<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Because the array is ordered, a single binary-search pass can locate both values in <code>O(log n)<\/code> time.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-d01492d1-56ef-4f23-8678-1c22329c7da9\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#0-brute-force-solution-linear-scan\" style=\"\">Brute Force Solution (Linear Scan)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#1-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#4-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#5-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#6-optimal-solution-single-binary-search\" style=\"\">Optimal Solution (Single Binary Search)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#7-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#8-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#9-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#10-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/ceil-the-floor\/#11-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-solution-linear-scan\">Brute Force Solution (Linear Scan)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-amp-approach\">Intuition &amp; Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Walk through every element.<\/li>\n\n\n\n<li>Track the <strong>largest value not exceeding<\/strong> <code>x<\/code> (floor) and the <strong>smallest value not less than<\/strong> <code>x<\/code> (ceil) as you go.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def getFloorAndCeil(a, n, x):\n    floor = -1           # greatest value \u2264 x found so far\n    ceil = -1            # smallest value \u2265 x found so far\n\n    for num in a:        # linear scan\n        if num &lt;= x:\n            if floor == -1 or num &gt; floor:\n                floor = num\n\n        if num &gt;= x:\n            if ceil == -1 or num &lt; ceil:\n                ceil = num\n\n    return floor, ceil\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">getFloorAndCeil<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">a<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    floor = -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">           <\/span><span style=\"color: #6A9955\"># greatest value \u2264 x found so far<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    ceil = -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># smallest value \u2265 x found so far<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> num <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> a:        <\/span><span style=\"color: #6A9955\"># linear scan<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num &lt;= x:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> floor == -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> num &gt; floor:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                floor = num<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num &gt;= x:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ceil == -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> num &lt; ceil:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                ceil = num<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> floor, ceil<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Two comparisons<\/strong> per element update <code>floor<\/code> and <code>ceil<\/code> whenever a better candidate is seen.<\/li>\n\n\n\n<li>After checking all <code>n<\/code> items, both answers are finalized.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(n)<\/code> \u2013 one full pass over the array.<\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(1)<\/code> \u2013 only two extra variables.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-conclusion\">Conclusion<\/h3>\n\n\n\n<p>Easy to implement but too slow for large arrays when a binary-search alternative exists.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-optimal-solution-single-binary-search\">Optimal Solution (Single Binary Search)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-intuition-amp-approach\">Intuition &amp; Approach<\/h3>\n\n\n\n<p>Use binary search to shrink the search space while <strong>simultaneously<\/strong> tracking best\u2010so\u2010far floor and ceil:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>low<\/code>, <code>high<\/code> bound the current window.<\/li>\n\n\n\n<li><code>mid = (low + high) \/\/ 2<\/code>.<\/li>\n\n\n\n<li><strong>If <code>a[mid] == x<\/code><\/strong> \u2192 exact match: both floor and ceil are <code>x<\/code>.<\/li>\n\n\n\n<li><strong>If <code>a[mid] > x<\/code><\/strong> \u2192 <code>a[mid]<\/code> is a <strong>candidate ceil<\/strong>; move left (<code>high = mid \u2013 1<\/code>) to search for a smaller one.<\/li>\n\n\n\n<li><strong>If <code>a[mid] &lt; x<\/code><\/strong> \u2192 <code>a[mid]<\/code> is a <strong>candidate floor<\/strong>; move right (<code>low = mid + 1<\/code>) to look for a larger one.<\/li>\n\n\n\n<li>Loop until <code>low > high<\/code>; remaining <code>floor<\/code> and <code>ceil<\/code> are the answers.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def getFloorAndCeil(a, n, x):\n    floor = -1      # best floor found so far (\u2264 x)\n    ceil = -1       # best ceil  found so far (\u2265 x)\n    n = len(a)      # array length (input n is not used here)\n\n    low, high = 0, n - 1         # binary-search boundaries\n\n    while low &lt;= high:\n        mid = (low + high) \/\/ 2  # midpoint of current window\n\n        if a[mid] == x:\n            return [x, x]        # exact match \u21d2 floor = ceil = x\n\n        elif a[mid] &gt; x:\n            ceil = a[mid]        # update candidate ceil\n            high = mid - 1       # search smaller indices for a tighter ceil\n\n        else:                    # a[mid] &lt; x\n            floor = a[mid]       # update candidate floor\n            low = mid + 1        # search larger indices for a tighter floor\n\n    return [floor, ceil]         # return final floor and ceil\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">getFloorAndCeil<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">a<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    floor = -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">      <\/span><span style=\"color: #6A9955\"># best floor found so far (\u2264 x)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    ceil = -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #6A9955\"># best ceil  found so far (\u2265 x)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(a)      <\/span><span style=\"color: #6A9955\"># array length (input n is not used here)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    low, high = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, n - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">         <\/span><span style=\"color: #6A9955\"># binary-search boundaries<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> low &lt;= high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        mid = (low + high) \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># midpoint of current window<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> a[mid] == x:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> [x, x]        <\/span><span style=\"color: #6A9955\"># exact match \u21d2 floor = ceil = x<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> a[mid] &gt; x:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            ceil = a[mid]        <\/span><span style=\"color: #6A9955\"># update candidate ceil<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            high = mid - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #6A9955\"># search smaller indices for a tighter ceil<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:                    <\/span><span style=\"color: #6A9955\"># a[mid] &lt; x<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            floor = a[mid]       <\/span><span style=\"color: #6A9955\"># update candidate floor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            low = mid + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># search larger indices for a tighter floor<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> [floor, ceil]         <\/span><span style=\"color: #6A9955\"># return final floor and ceil<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>floor<\/code><\/strong> captures the largest element encountered that\u2019s still \u2264 <code>x<\/code>.<\/li>\n\n\n\n<li><strong><code>ceil<\/code><\/strong> holds the smallest element encountered that\u2019s still \u2265 <code>x<\/code>.<\/li>\n\n\n\n<li>Each comparison discards half of the remaining indices, ensuring logarithmic performance.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(log n)<\/code> \u2013 classic binary-search behaviour.<\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(1)<\/code> \u2013 constant auxiliary storage.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-conclusion\">Conclusion<\/h3>\n\n\n\n<p>By adapting binary search to track both boundaries on the fly, we compute floor and ceil in logarithmic time with minimal code and memory, meeting the optimal efficiency for this problem.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a sorted array a[] of length n and a value x, you must return both: If either does not exist, output -1 in its place. Here&#8217;s the [Problem Link] to begin with. a (sorted) x Floor Ceil [1, 2, 8, 10, 11, 12, 19] 0 -1 1 [1, 2, 8, 10, 11, 12, 19]<\/p>\n","protected":false},"author":1,"featured_media":504,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,27],"class_list":{"0":"post-503","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-array","10":"tag-binary-search"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/ceil-the-floor-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=503"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/503\/revisions\/505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/504"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}