<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[find min in sorted array which is rotated by an unknown number of times]]></title><description><![CDATA[<p>hi,</p>
<p>i have an array which is sorted and rotated by an unknown number of times:<br />
22,28,30,1,2,4,10,12,14,20<br />
and i want to find the minimum.</p>
<p>i adapted binary search...but not sure if i handle all cases well?<br />
for case 1: im not sure about the search range:<br />
return get_min_in_rotation(vec, l, mid); ...is it mid or mid - 1</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;
#include &lt;algorithm&gt;
using namespace std;

// if the elements in the array are rotated by a unknown number of elements we can make the
// the following observations:
// case 1: arr[r] &gt; arr[mid] -&gt; continue in the left half
//     e.g. 22,28,30,1,2,4,10,12,14,20 
//           |         |             |
//           l        mid            r
//     - the left half is unsorted
//     - the right half is sorted 
//     -&gt; we keep looking for the min in the left half
//
// case 2: arr[r] &lt; arr[mid] -&gt; continue in the right half
//     e.g. 12,14,20,22,28,30,1,2,4,10
//           |          |            |
//           l         mid           r
//     - the left half is sorted
//     - the right half is unsorted 
//     -&gt; we keep looking for the min in the right half

int get_min_in_rotation(vector&lt;int&gt; &amp;vec, int l, int r) {
	// base case
	if (vec[l] &lt; vec[r]) return vec[l];

	int mid = l + (r - l) / 2;

	// check for rotation point
	if(vec[mid+1] &lt; vec[mid]) {
		return vec[mid+1];
	}
	// case 1: if the most right element is larger than the middle element, the min is in left half
	else if (vec[r] &gt; vec[mid]) {
		return get_min_in_rotation(vec, l, mid); // not sure if r = mid or mid - 1????
	}
	// case 2: if the most right element is smaller than the middle element, the min is in right half
	else if (vec[r] &lt; vec[mid]) {
		return get_min_in_rotation(vec, mid + 1, r);
	}
}

int main() {
	vector&lt;int&gt; vec = { 3, 4, 5, 1, 2 };

	for (int i = 0; i &lt; vec.size(); i++) {
		for_each(vec.begin(), vec.end(), [](int val) { cout &lt;&lt; val &lt;&lt; ' '; });
		cout &lt;&lt; '\n';

		cout &lt;&lt; &quot;min: &quot; &lt;&lt; get_min_in_rotation(vec, 0, vec.size() - 1) &lt;&lt; '\n' &lt;&lt; endl;

		rotate(vec.begin(), vec.end() - 1, vec.end());		
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/322570/find-min-in-sorted-array-which-is-rotated-by-an-unknown-number-of-times</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Jul 2026 19:54:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/322570.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 28 Dec 2013 22:06:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to find min in sorted array which is rotated by an unknown number of times on Sat, 28 Dec 2013 22:06:11 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>i have an array which is sorted and rotated by an unknown number of times:<br />
22,28,30,1,2,4,10,12,14,20<br />
and i want to find the minimum.</p>
<p>i adapted binary search...but not sure if i handle all cases well?<br />
for case 1: im not sure about the search range:<br />
return get_min_in_rotation(vec, l, mid); ...is it mid or mid - 1</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;numeric&gt;
#include &lt;algorithm&gt;
using namespace std;

// if the elements in the array are rotated by a unknown number of elements we can make the
// the following observations:
// case 1: arr[r] &gt; arr[mid] -&gt; continue in the left half
//     e.g. 22,28,30,1,2,4,10,12,14,20 
//           |         |             |
//           l        mid            r
//     - the left half is unsorted
//     - the right half is sorted 
//     -&gt; we keep looking for the min in the left half
//
// case 2: arr[r] &lt; arr[mid] -&gt; continue in the right half
//     e.g. 12,14,20,22,28,30,1,2,4,10
//           |          |            |
//           l         mid           r
//     - the left half is sorted
//     - the right half is unsorted 
//     -&gt; we keep looking for the min in the right half

int get_min_in_rotation(vector&lt;int&gt; &amp;vec, int l, int r) {
	// base case
	if (vec[l] &lt; vec[r]) return vec[l];

	int mid = l + (r - l) / 2;

	// check for rotation point
	if(vec[mid+1] &lt; vec[mid]) {
		return vec[mid+1];
	}
	// case 1: if the most right element is larger than the middle element, the min is in left half
	else if (vec[r] &gt; vec[mid]) {
		return get_min_in_rotation(vec, l, mid); // not sure if r = mid or mid - 1????
	}
	// case 2: if the most right element is smaller than the middle element, the min is in right half
	else if (vec[r] &lt; vec[mid]) {
		return get_min_in_rotation(vec, mid + 1, r);
	}
}

int main() {
	vector&lt;int&gt; vec = { 3, 4, 5, 1, 2 };

	for (int i = 0; i &lt; vec.size(); i++) {
		for_each(vec.begin(), vec.end(), [](int val) { cout &lt;&lt; val &lt;&lt; ' '; });
		cout &lt;&lt; '\n';

		cout &lt;&lt; &quot;min: &quot; &lt;&lt; get_min_in_rotation(vec, 0, vec.size() - 1) &lt;&lt; '\n' &lt;&lt; endl;

		rotate(vec.begin(), vec.end() - 1, vec.end());		
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2373961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373961</guid><dc:creator><![CDATA[c++coder11]]></dc:creator><pubDate>Sat, 28 Dec 2013 22:06:11 GMT</pubDate></item></channel></rss>