{"id":8105,"date":"2025-07-21T12:29:40","date_gmt":"2025-07-21T06:59:40","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=8105"},"modified":"2025-07-22T21:38:17","modified_gmt":"2025-07-22T16:08:17","slug":"square-root-of-x","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/square-root-of-x\/","title":{"rendered":"Square Root of X"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n\n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n\n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n\n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n\n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n\n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n\n  .wp_blog_explanation h1,\n  .wp_blog_explanation h2,\n  .wp_blog_explanation h3,\n  .wp_blog_explanation h4,\n  .wp_blog_explanation h5,\n  .wp_blog_explanation h6,\n  .wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n  }\n<\/style>\n\n<div class=\"wp_blog_explanation\">\n    <p>This problem is about finding the floor value of the square root of a non-negative integer <code>x<\/code>. You cannot use built-in square root functions. The goal is to use binary search to compute the answer efficiently.<\/p>\n  \n    <h2>Steps<\/h2>\n    <ul>\n      <li>If <code>x<\/code> is 0 or 1, return <code>x<\/code>.<\/li>\n      <li>Set the binary search range: <code>l = 2<\/code>, <code>r = floor(x \/ 2)<\/code>.<\/li>\n      <li>Use binary search to find the greatest number <code>m<\/code> such that <code>m * m \u2264 x<\/code>.<\/li>\n      <li>If <code>m * m === x<\/code>, return <code>m<\/code>.<\/li>\n      <li>If <code>m * m &gt; x<\/code>, search left side.<\/li>\n      <li>If <code>m * m &lt; x<\/code>, search right side.<\/li>\n      <li>Return <code>r<\/code> as the floor square root.<\/li>\n    <\/ul>\n  \n    <h2>Dry Run<\/h2>\n    <p><strong>Input:<\/strong> <code>x = 4<\/code><\/p>\n    <ol>\n      <li>l = 2, r = 2 \u2192 m = 2<\/li>\n      <li>2 * 2 = 4 \u2192 return 2<\/li>\n    <\/ol>\n    <p><strong>Output:<\/strong> <code>2<\/code><\/p>\n  \n    <p><strong>Input:<\/strong> <code>x = 8<\/code><\/p>\n    <ol>\n      <li>l = 2, r = 4 \u2192 m = 3 \u2192 3*3 = 9 &gt; 8 \u2192 r = 2<\/li>\n      <li>l = 2, r = 2 \u2192 m = 2 \u2192 2*2 = 4 &lt; 8 \u2192 l = 3<\/li>\n      <li>Loop ends \u2192 return r = 2<\/li>\n    <\/ol>\n    <p><strong>Output:<\/strong> <code>2<\/code><\/p>\n  \n    <h2>Time &#038; Space Complexity<\/h2>\n    <ul>\n      <li><strong>Time Complexity:<\/strong> O(log x)<\/li>\n      <li><strong>Space Complexity:<\/strong> O(1)<\/li>\n    <\/ul>\n  <\/div>\n  \n  <div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n      <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c#\">C#<\/button>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n      <pre><code class=\"language-javascript\">\n  \/**\n   * @param {number} x\n   * @return {number}\n   *\/\n  var mySqrt = function(x) {\n      if (x < 2) return x;\n      let l = 2;\n      let r = Math.floor(x \/ 2);\n      while (l <= r) {\n          let m = Math.floor((l + r) \/ 2);\n          if (x === m * m) {\n              return m;\n          } else if (x < m * m) {\n              r = m - 1;\n          } else {\n              l = m + 1;\n          }\n      }\n      return r;\n  };\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n      <pre><code class=\"language-cpp\">\n  #include &lt;iostream&gt;\n  using namespace std;\n  \n  int mySqrt(int x) {\n      if (x < 2) return x;\n      int l = 2, r = x \/ 2;\n      while (l <= r) {\n          int m = l + (r - l) \/ 2;\n          long long sq = 1LL * m * m;\n          if (sq == x) return m;\n          if (sq > x) r = m - 1;\n          else l = m + 1;\n      }\n      return r;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n      <pre><code class=\"language-c\">\n  #include &lt;stdio.h&gt;\n  \n  int mySqrt(int x) {\n      if (x < 2) return x;\n      int l = 2, r = x \/ 2;\n      while (l <= r) {\n          int m = l + (r - l) \/ 2;\n          long sq = (long)m * m;\n          if (sq == x) return m;\n          if (sq > x) r = m - 1;\n          else l = m + 1;\n      }\n      return r;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n      <pre><code class=\"language-java\">\n  public class Solution {\n      public int mySqrt(int x) {\n          if (x < 2) return x;\n          int l = 2, r = x \/ 2;\n          while (l <= r) {\n              int m = l + (r - l) \/ 2;\n              long sq = (long)m * m;\n              if (sq == x) return m;\n              if (sq > x) r = m - 1;\n              else l = m + 1;\n          }\n          return r;\n      }\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n      <pre><code class=\"language-python\">\n  def mySqrt(x: int) -> int:\n      if x < 2:\n          return x\n      l, r = 2, x \/\/ 2\n      while l <= r:\n          m = (l + r) \/\/ 2\n          if m * m == x:\n              return m\n          elif m * m > x:\n              r = m - 1\n          else:\n              l = m + 1\n      return r\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c#\">\n      <pre><code class=\"language-csharp\">\n  public class Solution {\n      public int MySqrt(int x) {\n          if (x < 2) return x;\n          int l = 2, r = x \/ 2;\n          while (l <= r) {\n              int m = l + (r - l) \/ 2;\n              long sq = (long)m * m;\n              if (sq == x) return m;\n              if (sq > x) r = m - 1;\n              else l = m + 1;\n          }\n          return r;\n      }\n  }\n      <\/code><\/pre>\n    <\/div>\n  <\/div>\n  \n\n\n  <a href=\"https:\/\/leetcode.com\/problems\/sqrtx\/description\/\" target=\"blank\">Solve this problem.<\/a>\n  <script>\n  document.addEventListener(\"DOMContentLoaded\", function () {\n    const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n    const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n    buttons.forEach((button) => {\n      button.addEventListener(\"click\", () => {\n        const lang = button.getAttribute(\"data-lang\");\n\n        buttons.forEach((btn) => btn.classList.remove(\"active\"));\n        button.classList.add(\"active\");\n\n        contents.forEach((content) => {\n          content.classList.toggle(\"active\", content.getAttribute(\"data-lang\") === lang);\n        });\n      });\n    });\n  });\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This problem is about finding the floor value of the square root of a non-negative integer x. You cannot use built-in square root functions. The goal is to use binary search to compute the answer efficiently. Steps If x is 0 or 1, return x. Set the binary search range: l = 2, r =<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[322,176,175,211,174,172,173],"tags":[],"class_list":["post-8105","post","type-post","status-publish","format-standard","category-algorithms-and-data-structures","category-csharp","category-cplusplus","category-data-structures","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=8105"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8105\/revisions"}],"predecessor-version":[{"id":8106,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8105\/revisions\/8106"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=8105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=8105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=8105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}