Skip to content

Commit bd6e49c

Browse files
author
Antesh Sharma
committed
merge two sorted array and find median
1 parent c7b408c commit bd6e49c

File tree

2 files changed

+93
-28
lines changed

2 files changed

+93
-28
lines changed

src/main/java/com/antesh/problems/DuplicateZeros.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public static void main(String[] args) {
2323
System.out.println(Arrays.toString(duplicateZerosOptimizedTCOn(new int[]{1, 0, 2, 3, 0, 4, 5, 0})));
2424

2525
}
26-
}
2726

2827
//TC: O(n) but SC: O(n)
2928
private static int[] duplicateZerosOptimizedTC(int[] arr) {
@@ -68,43 +67,40 @@ private static void shiftToRight(int[] arr, int start) {
6867
arr[end] = arr[end - 1];
6968
}
7069
}
71-
70+
7271
//TC: O(n*n) but SC: O(1)
7372
private static int[] duplicateZerosOptimizedTCOn(int[] arr) {
74-
7573
if (arr == null || arr.length == 0) {
7674
return new int[]{};
7775
}
78-
79-
int startShift = arr.length -1;
80-
for (int start = 0; start < arr.length; start++) {
81-
if (arr[start] == 0 && start < startShift) {
82-
// shiftToRight(arr, start);
83-
// start++;
84-
startShift--;
85-
}
86-
}
87-
88-
for (int end = arr.length-1; end >= 0; end++) {
8976

90-
if (arr[startShift ] != 0) {
91-
arr[end] = arr[startShift];
92-
// shiftToRight(arr, start);
77+
int startShiftingIndex = getStartShiftingIndex(arr);
78+
shiftElementsToRight(arr, startShiftingIndex);
9379

94-
// start++;
95-
96-
startShift--;
80+
return arr;
81+
}
9782

83+
private static void shiftElementsToRight(int[] arr, int startShiftingIndex) {
84+
for (int end = arr.length - 1; end >= 0; end--) {
85+
if (arr[startShiftingIndex] != 0) {
86+
arr[end] = arr[startShiftingIndex];
87+
startShiftingIndex--;
9888
} else {
99-
arr[end] = arr[startShift];
100-
101-
arr[end -1] = arr[startShift];
102-
starShift--;
103-
end -= 2;
104-
}
105-
89+
arr[end] = arr[startShiftingIndex];
90+
arr[end - 1] = arr[startShiftingIndex];
91+
startShiftingIndex--;
92+
end--;
93+
}
10694
}
95+
}
10796

108-
return arr;
97+
private static int getStartShiftingIndex(int[] arr) {
98+
int startShiftingIndex = arr.length - 1;
99+
for (int start = 0; start < arr.length; start++) {
100+
if (arr[start] == 0 && start < startShiftingIndex) {
101+
startShiftingIndex--;
102+
}
103+
}
104+
return startShiftingIndex;
109105
}
110106
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.antesh.problems;
2+
3+
import java.util.Arrays;
4+
5+
/* Merge 2 sorted arrays and find median
6+
7+
There are 2 sorted arrays A and B of size n each.
8+
Write an algorithm to find the median of the array
9+
obtained after merging the above 2 arrays(i.e. array of length 2n).
10+
11+
int ar1[] = {1, 12, 15, 26, 38};
12+
int ar2[] = {2, 13, 17, 30, 45};
13+
Median is 16
14+
15+
The complexity should be O(log(n)). for two equal length arrays
16+
*/
17+
public class MergeTwoSortedArraysAndFindMedianProblem {
18+
public static void main(String[] args) {
19+
int[] arr1 = {1, 12, 15, 26, 38};
20+
int[] arr2 = {2, 13, 17, 30, 45};
21+
22+
System.out.println("Median is " + mergeTwoSortedArrayAndFindMedian(arr1, 0, arr2, 0));
23+
24+
int[] arr3 = {1, 12, 15, 26};
25+
int[] arr4 = {2, 13, 17, 30, 45};
26+
27+
System.out.println("Median is " + mergeTwoSortedArrayAndFindMedian(arr3, 0, arr4, 0));
28+
29+
//If we already have sorted array and need to find median
30+
System.out.println(findMedian(new int[]{1, 2, 12, 13, 15, 24, 26, 38}));
31+
}
32+
33+
//TC: O(m+n)
34+
private static float mergeTwoSortedArrayAndFindMedian(int[] arr1, int m, int[] arr2, int n) {
35+
int[] sortedArrResult = new int[arr1.length + arr2.length];
36+
37+
int start = 0;
38+
while (m < arr1.length) {
39+
if (arr1[m] < arr2[n]) {
40+
sortedArrResult[start++] = arr1[m];
41+
m++;
42+
} else {
43+
sortedArrResult[start++] = arr2[n];
44+
n++;
45+
}
46+
}
47+
48+
while (n < arr2.length) {
49+
sortedArrResult[start++] = arr2[n++];
50+
}
51+
52+
System.out.println(Arrays.toString(sortedArrResult));
53+
54+
return findMedian(sortedArrResult);
55+
}
56+
57+
private static float findMedian(int[] sortedArr) {
58+
if (sortedArr.length % 2 == 0) {
59+
int n1 = (sortedArr.length - 1) / 2;
60+
int n2 = sortedArr.length / 2;
61+
return (float) (sortedArr[n1] + sortedArr[n2]) / 2;
62+
}
63+
64+
int n1 = (sortedArr.length - 1) / 2;
65+
int n2 = (sortedArr.length + 1) / 2;
66+
67+
return (float) (sortedArr[n1] + sortedArr[n2]) / 2;
68+
}
69+
}

0 commit comments

Comments
 (0)