Skip to content

Commit 1a06ba0

Browse files
committed
Updates
1 parent 99188b2 commit 1a06ba0

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Find Minimum in Rotated Sorted Array
2+
3+
## Problem Description
4+
5+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
6+
(i.e., `[0,1,2,4,5,6,7]` might become `[4,5,6,7,0,1,2]`).
7+
8+
Find the minimum element.
9+
10+
You may assume no duplicate exists in the array.
11+
12+
## Solution
13+
14+
This problem can be solved efficiently using a modified binary search approach.
15+
The key idea is to observe that if we pick an arbitrary element `mid`, one of the two halves (left or right) must be sorted.
16+
The minimum element will always be in the unsorted half.
17+
18+
1. Initialize `left` to 0 and `right` to `len(nums) - 1`.
19+
2. While `left < right`:
20+
a. Calculate `mid = left + (right - left) / 2`.
21+
b. If `nums[mid] > nums[right]`, it means the minimum element is in the right half (from `mid + 1` to `right`), because the right part is unsorted. So, set `left = mid + 1`.
22+
c. Else (`nums[mid] < nums[right]`), it means the minimum element is in the left half (from `left` to `mid`), because the right part is sorted, and `nums[mid]` could be the minimum. So, set `right = mid`.
23+
3. Return `nums[left]` (or `nums[right]`, as `left` and `right` will converge to the minimum element's index).
24+
25+
## Code (GoLang)
26+
27+
```go
28+
package main
29+
30+
import "fmt"
31+
32+
func findMin(nums []int) int {
33+
left, right := 0, len(nums)-1
34+
35+
for left < right {
36+
mid := left + (right-left)/2
37+
if nums[mid] > nums[right] {
38+
// Minimum is in the right half (mid+1 to right)
39+
left = mid + 1
40+
} else {
41+
// Minimum is in the left half (left to mid)
42+
// nums[mid] could be the minimum
43+
right = mid
44+
}
45+
}
46+
47+
return nums[left]
48+
}
49+
50+
func main() {
51+
fmt.Println(findMin([]int{3, 4, 5, 1, 2})) // Output: 1
52+
fmt.Println(findMin([]int{4, 5, 6, 7, 0, 1, 2})) // Output: 0
53+
fmt.Println(findMin([]int{1})) // Output: 1
54+
fmt.Println(findMin([]int{1, 2})) // Output: 1
55+
fmt.Println(findMin([]int{2, 1})) // Output: 1
56+
}
57+
```

posts/posts.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@
241241
"tags": ["cs", "algorithms", "graphs"],
242242
"series": {
243243
"posts": [
244+
{
245+
"slug": "find-minimum-in-rotated-sorted-array",
246+
"title": "Find Minimum in Rotated Sorted Array",
247+
"date": "2025-11-08",
248+
"description": "LeetCode problem: Find Minimum in Rotated Sorted Array",
249+
"tags": ["cs", "algorithms", "binary-search", "array"],
250+
"category": "dev",
251+
"filename": "/algos/find-minimum-in-rotated-sorted-array.txt"
252+
},
244253
{
245254
"slug": "lca",
246255
"title": "Lowest Common Ancestor with Binary Search Tree",

rss.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@
99
<link>https://fezcode.com</link>
1010
</image>
1111
<generator>RSS for Node</generator>
12-
<lastBuildDate>Fri, 07 Nov 2025 20:15:14 GMT</lastBuildDate>
12+
<lastBuildDate>Fri, 07 Nov 2025 21:29:36 GMT</lastBuildDate>
1313
<atom:link href="https://fezcode.com/rss.xml" rel="self" type="application/rss+xml"/>
14-
<pubDate>Fri, 07 Nov 2025 20:15:14 GMT</pubDate>
14+
<pubDate>Fri, 07 Nov 2025 21:29:36 GMT</pubDate>
1515
<copyright><![CDATA[2025 Ahmed Samil Bulbul]]></copyright>
1616
<language><![CDATA[en]]></language>
1717
<managingEditor><![CDATA[samil.bulbul@gmail.com (Ahmed Samil Bulbul)]]></managingEditor>
1818
<webMaster><![CDATA[samil.bulbul@gmail.com (Ahmed Samil Bulbul)]]></webMaster>
1919
<ttl>60</ttl>
20+
<item>
21+
<title><![CDATA[Find Minimum in Rotated Sorted Array]]></title>
22+
<description><![CDATA[[object Object]]]></description>
23+
<link>https://fezcode.com/#/blog/find-minimum-in-rotated-sorted-array</link>
24+
<guid isPermaLink="false">https://fezcode.com/#/blog/find-minimum-in-rotated-sorted-array</guid>
25+
<dc:creator><![CDATA[Ahmed Samil Bulbul]]></dc:creator>
26+
<pubDate>Sat, 08 Nov 2025 00:00:00 GMT</pubDate>
27+
<content:encoded><![CDATA[<h1>Find Minimum in Rotated Sorted Array</h1>
28+
<p><a href="https://fezcode.com/#/blog/find-minimum-in-rotated-sorted-array">Read more...</a></p>]]></content:encoded>
29+
</item>
2030
<item>
2131
<title><![CDATA[Demystifying Tailwind CSS]]></title>
2232
<description><![CDATA[[object Object]]]></description>

static/js/main.2280188f.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)