Skip to content

Commit 5626eb0

Browse files
Add Median of Two Sorted Arrays
1 parent 1fea05c commit 5626eb0

File tree

3 files changed

+72
-33
lines changed

3 files changed

+72
-33
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Median of Two Sorted Arrays
2+
3+
## LeetCode Problem
4+
- Problem Number: 4
5+
- Problem Link: https://leetcode.com/problems/median-of-two-sorted-arrays/
6+
- Difficulty: Hard
7+
- Topics: Array, Binary Search, Divide and Conquer
8+
9+
## Problem Description
10+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return the median of the two sorted arrays.
11+
12+
The overall run time complexity should be O(log (m+n)).
13+
14+
## Examples
15+
16+
### Example 1:
17+
```
18+
Input: nums1 = [1,3], nums2 = [2]
19+
Output: 2.00000
20+
Explanation: merged array = [1,2,3] and median is 2.
21+
```
22+
23+
### Example 2:
24+
```
25+
Input: nums1 = [1,2], nums2 = [3,4]
26+
Output: 2.50000
27+
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
28+
```
29+
30+
## Constraints
31+
- nums1.length == m
32+
- nums2.length == n
33+
- 0 <= m <= 1000
34+
- 0 <= n <= 1000
35+
- 1 <= m + n <= 2000
36+
- -10⁶ <= nums1[i], nums2[i] <= 10⁶
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
3+
if (nums1.length > nums2.length) {
4+
return findMedianSortedArrays(nums2, nums1);
5+
}
6+
7+
int m = nums1.length;
8+
int n = nums2.length;
9+
int low = 0, high = m;
10+
11+
while (low <= high) {
12+
int partition1 = (low + high) / 2;
13+
int partition2 = (m + n + 1) / 2 - partition1;
14+
15+
int maxLeft1 = (partition1 == 0) ? Integer.MIN_VALUE : nums1[partition1 - 1];
16+
int minRight1 = (partition1 == m) ? Integer.MAX_VALUE : nums1[partition1];
17+
18+
int maxLeft2 = (partition2 == 0) ? Integer.MIN_VALUE : nums2[partition2 - 1];
19+
int minRight2 = (partition2 == n) ? Integer.MAX_VALUE : nums2[partition2];
20+
21+
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
22+
if ((m + n) % 2 == 1) {
23+
return Math.max(maxLeft1, maxLeft2);
24+
}
25+
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;
26+
} else if (maxLeft1 > minRight2) {
27+
high = partition1 - 1;
28+
} else {
29+
low = partition1 + 1;
30+
}
31+
}
32+
33+
throw new IllegalArgumentException("Input arrays are not sorted or invalid.");
34+
}
35+
}

Two Sum/README.md

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,4 @@ Output: [0,1]
3939
- 2 <= nums.length <= 10⁴
4040
- -10⁹ <= nums[i] <= 10⁹
4141
- -10⁹ <= target <= 10⁹
42-
- Only one valid answer exists
43-
44-
## Solution Approach
45-
There are two main approaches to solve this problem:
46-
47-
### 1. Hash Table Approach (Optimal)
48-
- Time Complexity: O(n)
49-
- Space Complexity: O(n)
50-
- Algorithm:
51-
1. Create a HashMap to store complement values
52-
2. For each number in the array:
53-
- Calculate complement (target - current number)
54-
- If complement exists in HashMap, return [complementIndex, currentIndex]
55-
- Add current number and its index to HashMap
56-
57-
### 2. Brute Force Approach
58-
- Time Complexity: O(n²)
59-
- Space Complexity: O(1)
60-
- Algorithm:
61-
1. Use nested loops to try every possible pair
62-
2. Check if any pair sums to target
63-
3. Return indices when found
64-
65-
## Common Pitfalls
66-
1. Using the same element twice
67-
2. Not considering negative numbers
68-
3. Forgetting to check if complement exists before adding to HashMap
69-
4. Returning wrong order of indices
70-
71-
## Related Problems
72-
- 167: Two Sum II - Input Array Is Sorted
73-
- 170: Two Sum III - Data structure design
74-
- 653: Two Sum IV - Input is a BST
42+
- Only one valid answer exists

0 commit comments

Comments
 (0)