{"id":7967,"date":"2025-07-17T15:13:56","date_gmt":"2025-07-17T09:43:56","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7967"},"modified":"2025-07-17T20:47:23","modified_gmt":"2025-07-17T15:17:23","slug":"container-with-most-water","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/container-with-most-water\/","title":{"rendered":"Container With Most Water"},"content":{"rendered":"\n<!-- container with Most water. -->\n<link\n    href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\"\n    rel=\"stylesheet\"\n\/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.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_theme {\n  --primary: #E58C32;\n  --secondary: #f8c291;\n  --light-bg: #fef9f4;\n  --text-dark: #2d2d2d;\n  --tab-radius: 12px;\n  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);\n  --code-bg: #001f3f;\n  --code-text: #d4f1ff;\n}\n\n.wp_blog_container {\n  font-family: 'Segoe UI', sans-serif;\n  background: var(--light-bg);\n  margin: 0;\n  padding: 0;\n  color: var(--text-dark);\n}\n\n\/* Heading *\/\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: var(--primary);\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n\/* Explanation Card *\/\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  background: white;\n  border-radius: var(--tab-radius);\n  box-shadow: var(--shadow);\n}\n\n\/* Text and Visuals *\/\n.wp_blog_explanation h2{\n  font-size: 1.4rem;\n  color: var(--primary);\n  margin-bottom: 0.5rem;\n}\n.wp_blog_explanation h5{\n  color: var(--primary);\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n}\n.wp_blog_explanation code {\n  background: #f9cea6;\n  color: #2d2d2d;\n  padding: 3px 6px;\n  border-radius: 4px;\n  font-family: 'Courier New', monospace;\n}\n.wp_blog_explanation img {\n  max-width: 100%;\n  border-radius: var(--tab-radius);\n  margin-top: 1rem;\n  box-shadow: 0 2px 12px rgba(0,0,0,0.06);\n}\n\n\/* Tab Buttons *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid var(--primary);\n  background: white;\n  color: var(--primary);\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  transition: all 0.3s ease;\n}\n.wp_blog_code-tab-button:hover {\n  background: var(--secondary);\n}\n.wp_blog_code-tab-button.active {\n  background: var(--primary);\n  color: white;\n}\n\n\/* Code Content *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n}\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n  color: var(--code-text);\n}\n<\/style>\n\n<div class=\"wp_blog_container wp_blog_theme\"> \n    <h1 class=\"wp_blog_main-heading\"><\/h1>\n    <div class=\"wp_blog_explanation\">\n        <h2>Problem Statement:<\/h2>\n\n        <p>\n            You are given an integer array <code>height<\/code> of length <code>n<\/code>. There are <code>n<\/code> vertical lines drawn such that the two endpoints of the <code>i<\/code><sup>th<\/sup> line are <code>(i, 0)<\/code> and <code>(i, height[i])<\/code>.\n        <\/p>\n\n        <p>Find two lines that together with the x-axis form a container, such that the container contains the most water.<\/p>\n\n        <p>Return the <i>maximum amount of water a container<\/i> can store.<\/p>\n\n        <p><strong>Notice<\/strong> that you may not slant the container.<\/p>\n\n                <h2>Examples:<\/h2>\n\n                <h5>Example 1:<\/h5>\n                \n                <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-17-at-3.08.04\u202fPM.png\" alt=\"Stocks\" \/>\n                \n                <p><strong>Input:<\/strong>height = [1,8,6,2,5,4,8,3,7]<\/p>\n                <p><strong>Output:<\/strong>49<\/p>\n                <p>\n                  <code>Explanation:<\/code>\n                  The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.\n                <\/p>\n\n                <h5>Example 2:<\/h5>\n                <p><strong>Input:<\/strong>height = [1,1]<\/p>\n                <p><strong>Output:<\/strong>1<\/p>\n\n                    <h2>Constraints:<\/h2>\n                    <ul>\n                      <li><code>n == height.length<\/code><\/li>\n                      <li><code>2 <= n <= 10<sup>5<\/sup><\/code><\/li>\n                      <li><code>0 <= height[i] <= 10<sup>4<\/sup><\/code><\/li>\n                    <\/ul>\n\n                <h2>Approach<\/h2>\n                <ul>\n                    <li>Initialize two pointers <code>i = 0<\/code> and <code>j = arr.length - 1<\/code>.<\/li>\n                    <li>Calculate the area between the two lines at i and j:\n                        <p><code>area = min(arr[i], arr[j]) * (j - i)<\/code><\/p>\n                    <\/li>\n                    <li>Track the maximum area found so far.<\/li>\n                    <li>Move the pointer pointing to the shorter line inward (to potentially find a taller line and maximize area).<\/li>\n                    <li>Repeat until <code>i < j<\/code><\/li>\n                <\/ul>\n\n                <h2>Time Complexity:<\/h2>\n                <li>\n                  <p><strong>Time Complexity = O(n)<\/strong>\n                  <\/li>\n                <h2>Space Complexity:<\/h2>\n                <li>\n                  <p><strong>Space Complexity = O(1)<\/strong><\/p>\n                <\/li>\n\n<h2>Dry Run<\/h2> <div style=\"background: #f9f9f9; border-left: 4px solid var(--primary); padding: 1rem; border-radius: var(--tab-radius); margin: 1rem 0;\"> <p><strong>Input:<\/strong> <code>arr = [1, 8, 6, 2, 5, 4, 8, 3, 7]<\/code><\/p> <pre style=\"white-space: pre-wrap; background: #fff5ea; padding: 1rem; border-radius: 8px; overflow-x: auto;\"> i = 0, j = 8 \u2192 height = min(1, 7) = 1 \u2192 width = 8 \u2192 area = 8 \u2192 maxWater = 8 arr[i] < arr[j] \u2192 i++\ni = 1, j = 8 \u2192 height = min(8, 7) = 7 \u2192 width = 7 \u2192 area = 49 \u2192 maxWater = 49\narr[i] > arr[j] \u2192 j--\n\ni = 1, j = 7 \u2192 height = min(8, 3) = 3 \u2192 width = 6 \u2192 area = 18 \u2192 maxWater = 49\narr[i] > arr[j] \u2192 j--\n\ni = 1, j = 6 \u2192 height = min(8, 8) = 8 \u2192 width = 5 \u2192 area = 40 \u2192 maxWater = 49\narr[i] <= arr[j] \u2192 i++\n\ni = 2, j = 6 \u2192 height = min(6, 8) = 6 \u2192 width = 4 \u2192 area = 24 \u2192 maxWater = 49\narr[i] < arr[j] \u2192 i++\n\ni = 3, j = 6 \u2192 height = min(2, 8) = 2 \u2192 width = 3 \u2192 area = 6 \u2192 maxWater = 49\narr[i] < arr[j] \u2192 i++\n\ni = 4, j = 6 \u2192 height = min(5, 8) = 5 \u2192 width = 2 \u2192 area = 10 \u2192 maxWater = 49\narr[i] < arr[j] \u2192 i++\n\ni = 5, j = 6 \u2192 height = min(4, 8) = 4 \u2192 width = 1 \u2192 area = 4 \u2192 maxWater = 49\narr[i] < arr[j] \u2192 i++\n\ni = 6, j = 6 \u2192 loop ends\n<\/pre>\n\n<p><strong>Output:<\/strong> <code>Result: 49<\/code><\/p> <\/div>              \n        <!-- <h2>Visualisation:<\/h2>\n        <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/07\/Screenshot-2025-07-16-at-9.54.46\u202fAM.png\" alt=\"Stocks\" \/> -->\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=\"py\">Python<\/button>\n            <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/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=\"cs\">C#<\/button>\n        <\/div>\n\n        <!-- JavaScript -->\n        <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n            <pre><code class=\"language-javascript\">\nvar maxArea = function(arr) {\n    let i = 0;\n    let j = arr.length - 1;\n    let maxWater = 0;\n    while(i < j){\n        let area = Math.min(arr[i], arr[j]) * (j-i);\n        maxWater = Math.max(maxWater, area);\n        if(arr[i] > arr[j]) {\n            --j;\n        } else {\n            ++i;\n        }\n    }\n    return maxWater;\n};\n     <\/code><\/pre>\n        <\/div>\n\n        <!-- Python -->\n        <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n            <pre><code class=\"language-python\">\ndef maxArea(height):\n    i, j = 0, len(height) - 1\n    max_water = 0\n    while i < j:\n        area = min(height[i], height[j]) * (j - i)\n        max_water = max(max_water, area)\n        if height[i] > height[j]:\n            j -= 1\n        else:\n            i += 1\n    return max_water\n            <\/code><\/pre>\n        <\/div>\n\n        <!-- Java -->\n        <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n            <pre><code class=\"language-java\">\npublic int maxArea(int[] height) {\n    int i = 0, j = height.length - 1;\n    int maxWater = 0;\n    while (i < j) {\n        int area = Math.min(height[i], height[j]) * (j - i);\n        maxWater = Math.max(maxWater, area);\n        if (height[i] > height[j]) {\n            j--;\n        } else {\n            i++;\n        }\n    }\n    return maxWater;\n}     <\/code><\/pre>\n        <\/div>\n\n        <!-- C++ -->\n        <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n            <pre><code class=\"language-cpp\">\nint maxArea(vector<int>& height) {\n    int i = 0, j = height.size() - 1;\n    int maxWater = 0;\n    while (i < j) {\n        int area = min(height[i], height[j]) * (j - i);\n        maxWater = max(maxWater, area);\n        if (height[i] > height[j]) {\n            j--;\n        } else {\n            i++;\n        }\n    }\n    return maxWater;\n}   <\/code><\/pre>\n        <\/div>\n\n        <!-- C -->\n        <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n            <pre><code class=\"language-c\">\nint maxArea(int* height, int heightSize) {\n    int i = 0, j = heightSize - 1;\n    int maxWater = 0;\n    while (i < j) {\n        int h = height[i] < height[j] ? height[i] : height[j];\n        int area = h * (j - i);\n        if (area > maxWater) maxWater = area;\n        if (height[i] > height[j]) {\n            j--;\n        } else {\n            i++;\n        }\n    }\n    return maxWater;\n}\n       <\/code><\/pre>\n        <\/div>\n\n        <!-- C# -->\n        <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n            <pre><code class=\"language-csharp\">\npublic int MaxArea(int[] height) {\n    int i = 0, j = height.Length - 1;\n    int maxWater = 0;\n    while (i < j) {\n        int area = Math.Min(height[i], height[j]) * (j - i);\n        maxWater = Math.Max(maxWater, area);\n        if (height[i] > height[j]) {\n            j--;\n        } else {\n            i++;\n        }\n    }\n    return maxWater;\n}\n        <\/code><\/pre>\n        <\/div>\n    <\/div>\n<\/div>\n\n<script>\n    document.addEventListener(\"DOMContentLoaded\", () => {\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                contents.forEach((content) =>\n                    content.classList.remove(\"active\")\n                );\n\n                button.classList.add(\"active\");\n                document\n                    .querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`)\n                    .classList.add(\"active\");\n            });\n        });\n    });\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum<\/p>\n","protected":false},"author":108,"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,260,176,175,211,811,810,174,172,173],"tags":[],"class_list":["post-7967","post","type-post","status-publish","format-standard","category-algorithms-and-data-structures","category-c-c-plus-plus","category-csharp","category-cplusplus","category-data-structures","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7967","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\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7967"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7967\/revisions"}],"predecessor-version":[{"id":7969,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7967\/revisions\/7969"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}