Skip to content

Commit 8b70a46

Browse files
ADD Median-of-Two-Sorted-Arrays problem #53
1 parent 3b9c1ed commit 8b70a46

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public class Solution
6+
{
7+
public static void Main(string[] args)
8+
{
9+
int[] nums1 = { };
10+
int[] nums2 = { 1 };
11+
12+
Console.WriteLine(FindMedianSortedArrays(nums1, nums2));
13+
Console.WriteLine(FindMedianSortedArraysUsingBinarySearch(nums1, nums2));
14+
15+
Console.ReadKey();
16+
}
17+
18+
private static double _GetMedian(double min, double max)
19+
=> min + ((max - min) / 2);
20+
21+
private static bool _IsEven(int num)
22+
=> (num % 2 == 0);
23+
24+
#region Approach 1
25+
public static double FindMedianSortedArrays(int[] nums1, int[] nums2)
26+
{
27+
if (nums1 == null || nums2 == null)
28+
{
29+
return double.NaN;
30+
}
31+
32+
List<int> list1 = new List<int>(nums1);
33+
List<int> list2 = new List<int>(nums2);
34+
35+
var list3 = list1.Concat(list2).OrderBy(x => x).ToList();
36+
37+
int listCount = list3.Count;
38+
39+
if (_IsEven(listCount))
40+
{
41+
int lastIndexInMedian = listCount / 2;
42+
int firstIndexMedian = lastIndexInMedian - 1;
43+
44+
return _GetMedian(list3[firstIndexMedian], list3[lastIndexInMedian]);
45+
}
46+
else
47+
{
48+
return list3[listCount / 2];
49+
}
50+
}
51+
#endregion
52+
53+
#region Approach 2
54+
private static double _PerformBinarySearchToGetResult(int[] nums1, int[] nums2)
55+
{
56+
int m = nums1.Length;
57+
int n = nums2.Length;
58+
int total = m + n;
59+
int half = (total + 1) / 2;
60+
int left = 0;
61+
int right = m;
62+
double result = 0.0;
63+
while (left <= right)
64+
{
65+
int middle = left + (right - left) / 2;
66+
int j = half - middle;
67+
int left1 = (middle > 0) ? nums1[middle - 1] : int.MinValue;
68+
int right1 = (middle < m) ? nums1[middle] : int.MaxValue;
69+
int left2 = (j > 0) ? nums2[j - 1] : int.MinValue;
70+
int right2 = (j < n) ? nums2[j] : int.MaxValue;
71+
72+
if (left1 <= right2 && left2 <= right1)
73+
{
74+
if (_IsEven(total))
75+
{
76+
result = (Math.Max(left1, left2) + Math.Min(right1, right2)) / 2.0;
77+
}
78+
else
79+
{
80+
result = Math.Max(left1, left2);
81+
}
82+
break;
83+
}
84+
else if (left1 > right2)
85+
{
86+
right = middle - 1;
87+
}
88+
else
89+
{
90+
left = middle + 1;
91+
}
92+
}
93+
return result;
94+
}
95+
96+
public static double FindMedianSortedArraysUsingBinarySearch(int[] nums1, int[] nums2)
97+
{
98+
if (nums1.Length <= 0 && nums2.Length == 1)
99+
{
100+
return nums2[0];
101+
}
102+
if (nums2.Length <= 0 && nums1.Length == 1)
103+
{
104+
return nums1[0];
105+
}
106+
107+
if (nums1.Length > nums2.Length)
108+
{
109+
// swapping
110+
(nums1, nums2) = (nums2, nums1);
111+
//return FindMedianSortedArrays(nums2, nums1);
112+
}
113+
114+
return _PerformBinarySearchToGetResult(nums1, nums2);
115+
}
116+
#endregion
117+
}
118+

0 commit comments

Comments
 (0)