<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vishva Thejeshwar</title>
    <description>The latest articles on DEV Community by Vishva Thejeshwar (@hvaminion).</description>
    <link>https://dev.to/hvaminion</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F110826%2F078d2939-7902-4a8d-a4a0-56909e72ee37.jpg</url>
      <title>DEV Community: Vishva Thejeshwar</title>
      <link>https://dev.to/hvaminion</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hvaminion"/>
    <language>en</language>
    <item>
      <title>Find the smallest number in a rotated sorted array</title>
      <dc:creator>Vishva Thejeshwar</dc:creator>
      <pubDate>Wed, 01 May 2019 06:08:23 +0000</pubDate>
      <link>https://dev.to/hvaminion/find-the-smallest-number-in-a-rotated-sorted-array-2pbd</link>
      <guid>https://dev.to/hvaminion/find-the-smallest-number-in-a-rotated-sorted-array-2pbd</guid>
      <description>

&lt;p&gt;Question link : &lt;a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/"&gt;https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rotated sorted array : a sorted array which is rotated ( ¯\_(ツ)_/¯ ). &lt;/p&gt;

&lt;p&gt;Leet code has a better definition : an array sorted in ascending order is rotated at some pivot&lt;/p&gt;

&lt;p&gt;Sorted array : [1,2,3,4,5,6,7]&lt;br&gt;
Rotated sorted array :&lt;br&gt;
1 rotation  : [7,1,2,3,4,5,6]&lt;br&gt;
2 rotations : [6,7,1,2,3,4,5]&lt;br&gt;
3 rotations : [5,6,7,1,2,3,4]&lt;br&gt;
7 rotations : [1,2,3,4,5,6,7] - the same sorted array &lt;/p&gt;

&lt;p&gt;To find the smallest element in the given array :&lt;/p&gt;

&lt;p&gt;Approach 1 : Brute force - Go through the array once and find the minimum element - O(n) &lt;/p&gt;

&lt;p&gt;Approach 2 :  Binary Search O(log n)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If the number of elements is 1 , return the element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose a element at a mid point&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If the first element of the array is greater than mid element, then the minimum element should be in between the first element and mid element&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example : [6,7,0,1,2,3,4]
mid element is 1
first element is 6
smallest element 0 is between first element and mid element [7,0,1]
The first element can be excluded because we already know that mid element is smaller than the first and we only need smallest element
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If the first element is smaller than the n, then two possibilities arise:&lt;br&gt;
4.1  The array may already be sorted : Check if the last element is greater than the element at mid, if yes return the first element &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example : [1,2,3,4,5,6,7]
mid element is 4
first element is 1
last element is 7
last element is greater than the mid, so return the first element i.e.1 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;4.2   The minimum element is present between the mid element and the end of the array &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example : [2,4,5,6,7,0,1]
mid element is 6
first element is 2
last element is 1
last element is lesser than the mid, so smallest element is between mid and the end [7,0,1] 
the mid element can be excluded because we already know the last element is lesser than the mid
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Final code :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;


</description>
      <category>avishvatblog2</category>
    </item>
  </channel>
</rss>
