You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0000-0099/0004-Median-of-two-Sorted-Array.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ tags:
6
6
- Array
7
7
- Binary Search
8
8
- Divide and Conquer
9
-
description: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
9
+
description: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be $O(log (m+n))$.
10
10
---
11
11
12
12
@@ -20,7 +20,7 @@ description: Given two sorted arrays nums1 and nums2 of size m and n respectivel
20
20
21
21
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return the median of the two sorted arrays.
22
22
23
-
The overall run time complexity should be `O(log (m+n))`.
23
+
The overall run time complexity should be $O(log (m+n))$.
24
24
25
25
**Example 1:**
26
26
```
@@ -46,7 +46,7 @@ Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
46
46
47
47
### Solution
48
48
49
-
#### Approach 1: Merge Sort (O(m + n))
49
+
#### Approach 1: Merge Sort $(O(m + n))$
50
50
51
51
**Algorithm:**
52
52
1. Merge `nums1` and `nums2` into a single sorted array.
To achieve O(log(min(m, n))) complexity, use binary search on the smaller array. The goal is to find a partition where the left half of both arrays combined is less than or equal to the right half.
82
+
To achieve $O(log(min(m, n)))$ complexity, use binary search on the smaller array. The goal is to find a partition where the left half of both arrays combined is less than or equal to the right half.
- Approach 1:O(m + n) because it involves merging both arrays into one sorted array.
129
-
- Approach 2: O(log(min(m, n))) because it performs binary search on the smaller array.
128
+
- Approach 1:$O(m + n)$ because it involves merging both arrays into one sorted array.
129
+
- Approach 2: $O(log(min(m, n)))$ because it performs binary search on the smaller array.
130
130
131
131
-**Space Complexity:**
132
-
- Approach 1: O(m + n) due to the additional space needed for the merged array.
133
-
- Approach 2: O(1) because it uses only a constant amount of additional space.
132
+
- Approach 1: $O(m + n)$ due to the additional space needed for the merged array.
133
+
- Approach 2: $O(1)$ because it uses only a constant amount of additional space.
134
134
135
135
### Summary
136
136
137
-
Approach 1 is straightforward but not optimal in terms of time complexity for large input sizes. Approach 2 leverages binary search to efficiently find the median with logarithmic time complexity, making it suitable for large arrays.
137
+
Approach 1 is straightforward but not optimal in terms of time complexity for large input sizes. Approach 2 leverages binary search to efficiently find the median with logarithmic time complexity, making it suitable for large arrays.
0 commit comments