{"id":566,"date":"2025-07-09T17:31:59","date_gmt":"2025-07-09T12:01:59","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=566"},"modified":"2025-07-09T17:32:01","modified_gmt":"2025-07-09T12:02:01","slug":"find-minimum-in-rotated-sorted-array","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/","title":{"rendered":"Find Minimum in Rotated Sorted Array | Leetcode 153 | Optimal Binary Search"},"content":{"rendered":"\n<p>The &#8220;Find Minimum in Rotated Sorted Array&#8221; problem is a classic question that tests your understanding of binary search and array manipulation. In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Suppose an array of length&nbsp;<code>n<\/code>&nbsp;sorted in ascending order is&nbsp;<strong>rotated<\/strong>&nbsp;between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>n<\/code>&nbsp;times. For example, the array&nbsp;<code>nums = [0,1,2,4,5,6,7]<\/code>&nbsp;might become:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>[4,5,6,7,0,1,2]<\/code>\u00a0if it was rotated\u00a0<code>4<\/code>\u00a0times.<\/li>\n\n\n\n<li><code>[0,1,2,4,5,6,7]<\/code>\u00a0if it was rotated\u00a0<code>7<\/code>\u00a0times.<\/li>\n<\/ul>\n\n\n\n<p>Notice that&nbsp;<strong>rotating<\/strong>&nbsp;an array&nbsp;<code>[a[0], a[1], a[2], ..., a[n-1]]<\/code>&nbsp;1 time results in the array&nbsp;<code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]<\/code>.<\/p>\n\n\n\n<p>Given the sorted rotated array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;<strong>unique<\/strong>&nbsp;elements, return&nbsp;<em>the minimum element of this array<\/em>.<\/p>\n\n\n\n<p>You must write an algorithm that runs in&nbsp;<code>O(log n) time<\/code>.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/find-minimum-in-rotated-sorted-array\/description\/\" 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<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums = [3,4,5,1,2]<br><strong>Output:<\/strong> 1<br><strong>Explanation:<\/strong> The original array was [1,2,3,4,5] rotated 3 times.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums = [4,5,6,7,0,1,2]<br><strong>Output:<\/strong> 0<br><strong>Explanation:<\/strong> The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums = [11,13,15,17]<br><strong>Output:<\/strong> 11<br><strong>Explanation:<\/strong> The original array was [11,13,15,17] and it was rotated 4 times. <\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>n == nums.length<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= 5000<\/code><\/li>\n\n\n\n<li><code>-5000 &lt;= nums[i] &lt;= 5000<\/code><\/li>\n\n\n\n<li>All the integers of\u00a0<code>nums<\/code>\u00a0are\u00a0<strong>unique<\/strong>.<\/li>\n\n\n\n<li><code>nums<\/code>\u00a0is sorted and rotated between\u00a0<code>1<\/code>\u00a0and\u00a0<code>n<\/code>\u00a0times.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-089c5d4f-f981-4852-aa76-6e8f96abab4b\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"false\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><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\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 \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#0-brute-force-solution-linear-scan\" style=\"\">Brute Force Solution (Linear Scan)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#1-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#4-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#5-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#7-optimal-solution-binary-search\" style=\"\">Optimal Solution (Binary Search)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#8-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#10-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#11-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#12-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#13-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-minimum-in-rotated-sorted-array\/#14-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\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-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>Scan Each Element:<\/strong><br>Go through the array from start to end.<\/li>\n\n\n\n<li><strong>Track the Minimum:<\/strong><br>Keep updating a variable with the smallest value found so far.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every element, so we\u2019re sure to find the minimum.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large arrays.<\/p>\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(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:#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=\"class Solution:\n    def findMin(self, nums: List[int]) -&gt; int:\n        minimum = float(&quot;inf&quot;)                # Initialize minimum as infinity\n        for i in range(len(nums)):            # Iterate through the array\n            minimum = min(minimum, nums[i])   # Update minimum if needed\n        return minimum                        # Return the smallest element\" 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\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">findMin<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        minimum = <\/span><span style=\"color: #4EC9B0\">float<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;inf&quot;<\/span><span style=\"color: #D4D4D4\">)                <\/span><span style=\"color: #6A9955\"># Initialize minimum as infinity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)):            <\/span><span style=\"color: #6A9955\"># Iterate through the array<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            minimum = <\/span><span style=\"color: #DCDCAA\">min<\/span><span style=\"color: #D4D4D4\">(minimum, nums[i])   <\/span><span style=\"color: #6A9955\"># Update minimum if needed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> minimum                        <\/span><span style=\"color: #6A9955\"># Return the smallest element<\/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>We initialize\u00a0<code>minimum<\/code>\u00a0as infinity.<\/li>\n\n\n\n<li>We loop through each element in the array.<\/li>\n\n\n\n<li>If we find a smaller value, we update\u00a0<code>minimum<\/code>.<\/li>\n\n\n\n<li>After checking all elements, we return\u00a0<code>minimum<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>minimum = inf<\/li>\n\n\n\n<li>Compare 4 \u2192 minimum = 4<\/li>\n\n\n\n<li>Compare 5 \u2192 minimum = 4<\/li>\n\n\n\n<li>Compare 6 \u2192 minimum = 4<\/li>\n\n\n\n<li>Compare 7 \u2192 minimum = 4<\/li>\n\n\n\n<li>Compare 0 \u2192 minimum = 0 (updated)<\/li>\n\n\n\n<li>Compare 1 \u2192 minimum = 0<\/li>\n\n\n\n<li>Compare 2 \u2192 minimum = 0<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>0<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-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 element.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only a variable for the minimum.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small arrays, 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=\"7-optimal-solution-binary-search\">Optimal Solution (Binary Search)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-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:<\/strong><br>Use two pointers (<code>low<\/code>\u00a0and\u00a0<code>high<\/code>) to perform binary search.<\/li>\n\n\n\n<li><strong>Check Sorted Halves:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the left half is sorted, the minimum is either at\u00a0<code>low<\/code>\u00a0or in the right half.<\/li>\n\n\n\n<li>If the right half is sorted, the minimum is either at\u00a0<code>mid<\/code>\u00a0or in the left half.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>In a rotated sorted array, the minimum element is the only element where the previous element is greater than it. Binary search helps us find this quickly.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-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=\"class Solution:\n    def findMin(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        low = 0\n        high = n - 1\n        minimum = float(&quot;inf&quot;)\n\n        while low &lt;= high:\n            mid = (low + high) \/\/ 2            # Find the middle index\n\n            # If left half is sorted\n            if nums[low] &lt;= nums[mid]:\n                minimum = min(minimum, nums[low])  # Update minimum\n                low = mid + 1                      # Search in the right half\n            else:                                  # Right half is sorted\n                minimum = min(minimum, nums[mid])  # Update minimum\n                high = mid - 1                     # Search in the left half\n\n        return minimum                             # Return the smallest element\" 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\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">findMin<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        low = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        high = n - <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        minimum = <\/span><span style=\"color: #4EC9B0\">float<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;inf&quot;<\/span><span style=\"color: #D4D4D4\">)<\/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\"># Find the middle index<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># If left half is sorted<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[low] &lt;= nums[mid]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                minimum = <\/span><span style=\"color: #DCDCAA\">min<\/span><span style=\"color: #D4D4D4\">(minimum, nums[low])  <\/span><span style=\"color: #6A9955\"># Update minimum<\/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 in the right half<\/span><\/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\"># Right half is sorted<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                minimum = <\/span><span style=\"color: #DCDCAA\">min<\/span><span style=\"color: #D4D4D4\">(minimum, nums[mid])  <\/span><span style=\"color: #6A9955\"># Update minimum<\/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 in the left half<\/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\"> minimum                             <\/span><span style=\"color: #6A9955\"># Return the smallest element<\/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=\"10-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use binary search with pointers\u00a0<code>low<\/code>\u00a0and\u00a0<code>high<\/code>.<\/li>\n\n\n\n<li>If the left half is sorted, the minimum must be at\u00a0<code>low<\/code>\u00a0or in the right half.<\/li>\n\n\n\n<li>If the right half is sorted, the minimum must be at\u00a0<code>mid<\/code>\u00a0or in the left half.<\/li>\n\n\n\n<li>We keep updating\u00a0<code>minimum<\/code>\u00a0and narrowing our search window.<\/li>\n\n\n\n<li>When the search is done, we return\u00a0<code>minimum<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>low = 0, high = 6<\/li>\n\n\n\n<li>mid = 3 (nums = 7)<\/li>\n\n\n\n<li>Left half is sorted (nums=4 &lt;= nums=7)<\/li>\n\n\n\n<li>minimum = min(inf, 4) = 4<\/li>\n\n\n\n<li>Move low to mid + 1 = 4<\/li>\n\n\n\n<li>low = 4, high = 6<\/li>\n\n\n\n<li>mid = 5 (nums = 1)<\/li>\n\n\n\n<li>Left half is sorted (nums=0 &lt;= nums=1)<\/li>\n\n\n\n<li>minimum = min(4, 0) = 0<\/li>\n\n\n\n<li>Move low to mid + 1 = 6<\/li>\n\n\n\n<li>low = 6, high = 6<\/li>\n\n\n\n<li>mid = 6 (nums = 2)<\/li>\n\n\n\n<li>Left half is sorted (nums=2 &lt;= nums=2)<\/li>\n\n\n\n<li>minimum = min(0, 2) = 0<\/li>\n\n\n\n<li>Move low to mid + 1 = 7<\/li>\n\n\n\n<li>low > high, exit loop.<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>0<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-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 array each time.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only a few pointers and a variable for the minimum.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and works well for large arrays. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Find Minimum in Rotated Sorted Array&#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;Find Minimum in Rotated Sorted Array&#8221; problem is a classic question that tests your understanding of binary search and array manipulation. In this blog, we\u2019ll explain the problem, walk you through both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations. Suppose an array<\/p>\n","protected":false},"author":1,"featured_media":568,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[7,27,19],"class_list":{"0":"post-566","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-array","10":"tag-binary-search","11":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/find-minimum-in-rotated-sorted-array-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\/566","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=566"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/566\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/566\/revisions\/569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/568"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}