{"id":581,"date":"2025-07-09T18:18:04","date_gmt":"2025-07-09T12:48:04","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=581"},"modified":"2025-07-09T18:18:06","modified_gmt":"2025-07-09T12:48:06","slug":"square-root-using-binary-search","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/","title":{"rendered":"Square Root using Binary Search \u2013 GeeksforGeeks Solution Explained"},"content":{"rendered":"\n<p>The &#8220;Square Root using Binary Search&#8221; problem is a classic question that helps you understand both brute force and binary search techniques. In this blog, we\u2019ll explain the problem, walk you through both brute force and binary search solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/square-root\/0\" 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<h2 class=\"wp-block-heading\" id=\"0-what-does-the-problem-say\">What Does the Problem Say?<\/h2>\n\n\n\n<p>You are given a non-negative integer&nbsp;<code>n<\/code>. Your task is to compute the&nbsp;<strong>floor value of the square root of&nbsp;<code>n<\/code><\/strong>&nbsp;(i.e., the greatest integer&nbsp;<code>x<\/code>&nbsp;such that&nbsp;<code>x*x &lt;= n<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-example-1\">Example 1<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>n = 5<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>2<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong><br>The square root of 5 is approximately 2.236. The floor value is 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-example-2\">Example 2<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>n = 16<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>4<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong><br>The square root of 16 is exactly 4.<\/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-fe901c5b-c81f-4e1b-960b-40491ada21f3\" 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\/square-root-using-binary-search\/#0-what-does-the-problem-say\" style=\"\">What Does the Problem Say?<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#1-example-1\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#2-example-2\" style=\"\">Example 2<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#3-brute-force-solution-linear-scan\" style=\"\">Brute Force Solution (Linear Scan)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#4-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#5-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#6-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#7-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#8-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#9-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#10-solution-using-binary-search\" style=\"\">Solution using Binary Search<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#11-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#12-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#13-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#14-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#15-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#16-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/square-root-using-binary-search\/#17-final-thoughts\" style=\"\">Final Thoughts<\/a><\/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=\"3-brute-force-solution-linear-scan\">Brute Force Solution (Linear Scan)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem in the most straightforward way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Try Every Number:<\/strong><br>Start from 0 and go up to n. For each number, check if its square is less than or equal to n.<\/li>\n\n\n\n<li><strong>Keep Track of the Answer:<\/strong><br>If the square is less than or equal to n, update the answer.<\/li>\n\n\n\n<li><strong>Stop When Square Exceeds n:<\/strong><br>As soon as the square of the number is greater than n, break the loop.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every possible integer, so we\u2019re sure to find the largest integer whose square is less than or equal to n.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not very efficient for large values of n.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-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:#e1e4e8;--cbp-line-number-width:calc(1 * 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:#24292e\"><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=\"class Solution:\n    def floorSqrt(self, n):\n        ans = 0\n        for i in range(0, n + 1):\n            if i * i &lt;= n:      # Check if square is less than or equal to n\n                ans = i         # Update answer\n            else:\n                break           # Stop if square exceeds n\n        return ans              # Return the floor value of sqrt(n)\" style=\"color:#e1e4e8;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 github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">floorSqrt<\/span><span style=\"color: #E1E4E8\">(self, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        ans <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">, n <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">*<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> n:      <\/span><span style=\"color: #6A737D\"># Check if square is less than or equal to n<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                ans <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> i         <\/span><span style=\"color: #6A737D\"># Update answer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">break<\/span><span style=\"color: #E1E4E8\">           <\/span><span style=\"color: #6A737D\"># Stop if square exceeds n<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> ans              <\/span><span style=\"color: #6A737D\"># Return the floor value of sqrt(n)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start from 0 and check every number up to n.<\/li>\n\n\n\n<li>If\u00a0<code>i * i<\/code>\u00a0is less than or equal to n, update\u00a0<code>ans<\/code>.<\/li>\n\n\n\n<li>As soon as\u00a0<code>i * i<\/code>\u00a0exceeds n, break the loop.<\/li>\n\n\n\n<li>Return the last valid value of\u00a0<code>ans<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>n = 10<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>i = 0: 0*0 = 0 \u2264 10 \u2192 ans = 0<\/li>\n\n\n\n<li>i = 1: 1*1 = 1 \u2264 10 \u2192 ans = 1<\/li>\n\n\n\n<li>i = 2: 2*2 = 4 \u2264 10 \u2192 ans = 2<\/li>\n\n\n\n<li>i = 3: 3*3 = 9 \u2264 10 \u2192 ans = 3<\/li>\n\n\n\n<li>i = 4: 4*4 = 16 > 10 \u2192 break<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>3<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n)<br>We may have to check every number up to n.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only a variable for the answer.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small values of n, but it\u2019s too slow for large inputs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-solution-using-binary-search\">Solution using Binary Search<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>We can do much better using binary search:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Search Range:<\/strong><br>The answer lies between 0 and n. Use binary search to find the largest integer whose square is less than or equal to n.<\/li>\n\n\n\n<li><strong>Check the Middle Value:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If\u00a0<code>mid*mid<\/code>\u00a0is less than or equal to n, move the search to the right (higher values).<\/li>\n\n\n\n<li>If\u00a0<code>mid*mid<\/code>\u00a0is greater than n, move the search to the left (lower values).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>Binary search efficiently narrows down the possible values, making it much faster for large n.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-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:#e1e4e8;--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:#24292e\"><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=\"class Solution:\n    def floorSqrt(self, n):\n        low = 0\n        high = n\n        while low &lt;= high:\n            mid = (low + high) \/\/ 2\n            if mid * mid &lt;= n:\n                low = mid + 1        # Try for a bigger answer\n            else:\n                high = mid - 1       # Try for a smaller answer\n        return high                  # high will be the floor value of sqrt(n)\" style=\"color:#e1e4e8;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 github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">floorSqrt<\/span><span style=\"color: #E1E4E8\">(self, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> n<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> low <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (low <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> high) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">*<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Try for a bigger answer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">       <\/span><span style=\"color: #6A737D\"># Try for a smaller answer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> high                  <\/span><span style=\"color: #6A737D\"># high will be the floor value of sqrt(n)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use binary search between 0 and n.<\/li>\n\n\n\n<li>If\u00a0<code>mid*mid<\/code>\u00a0is less than or equal to n, move\u00a0<code>low<\/code>\u00a0up to search for a bigger answer.<\/li>\n\n\n\n<li>If\u00a0<code>mid*mid<\/code>\u00a0is greater than n, move\u00a0<code>high<\/code>\u00a0down to search for a smaller answer.<\/li>\n\n\n\n<li>When the loop ends,\u00a0<code>high<\/code>\u00a0will be the largest integer whose square is less than or equal to n.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>n = 10<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>low = 0, high = 10<\/li>\n\n\n\n<li>mid = 5, 5*5 = 25 > 10 \u2192 high = 4<\/li>\n\n\n\n<li>mid = 2, 2*2 = 4 \u2264 10 \u2192 low = 3<\/li>\n\n\n\n<li>mid = 3, 3*3 = 9 \u2264 10 \u2192 low = 4<\/li>\n\n\n\n<li>mid = 4, 4*4 = 16 > 10 \u2192 high = 3<\/li>\n<\/ul>\n\n\n\n<p>Loop ends.<br><strong>Final Output:<\/strong><br><code>3<\/code>&nbsp;(since 3<em>3 = 9 \u2264 10 and 4<\/em>4 = 16 &gt; 10)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(log n)<br>Binary search divides the range each time.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only a few pointers.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The binary search solution is efficient and works well for large values of n. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Square Root using Binary Search&#8221; problem is a great example of how to optimize from brute force to an efficient binary search solution. Start with brute force to understand the basics, then use binary search for best performance.<\/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>The &#8220;Square Root using Binary Search&#8221; problem is a classic question that helps you understand both brute force and binary search techniques. In this blog, we\u2019ll explain the problem, walk you through both brute force and binary search solutions, and make everything easy to understand with code comments, dry runs, and clear explanations. Here&#8217;s the<\/p>\n","protected":false},"author":1,"featured_media":582,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[28,8],"class_list":{"0":"post-581","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-binary-search-on-answers","10":"tag-easy"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/square-root-using-binary-search-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\/581","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=581"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":583,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/581\/revisions\/583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/582"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}