Skip to content

Commit af466da

Browse files
author
Antesh Sharma
committed
refactoring
1 parent 6a56692 commit af466da

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.antesh.dsa.arrays;
2+
3+
import java.util.Arrays;
4+
5+
/* Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements to right
6+
*
7+
* Sample 1: { 1, 2, 3, 4, 5, 6, 7 }, d = 2, rotate right
8+
* output 1: { 6, 7, 1, 2, 3, 4, 5 }
9+
*
10+
* Sample 1: { 1, 2, 3, 4, 5, 6, 7 }, d = 2, rotate left
11+
* output 1: { 3, 4, 5, 6, 7, 1, 2 }
12+
* */
13+
public class ArrayRotation {
14+
15+
//TC: O(n*d)
16+
public static void rotateRight(int[] arr, int d) {
17+
if (arr == null || arr.length == 0) {
18+
return;
19+
}
20+
int n = arr.length;
21+
int j = 0;
22+
while (j < d) {
23+
rotateByOnePosition(arr, n);
24+
j++;
25+
}
26+
}
27+
28+
private static void rotateByOnePosition(int[] arr, int n) {
29+
int temp = arr[n - 1];
30+
for (int i = 0; i < n; i++) {
31+
int current = arr[i];
32+
arr[i] = temp;
33+
temp = current;
34+
}
35+
}
36+
37+
public static void rotateLeft(int[] arr, int d) {
38+
if (arr == null || arr.length == 0) {
39+
return;
40+
}
41+
int n = arr.length;
42+
rotateRight(arr, n-d);
43+
}
44+
45+
public static void main(String[] args) {
46+
int[] arr = {1, 2, 3, 4, 5, 6, 7};
47+
rotateRight(arr, 1);
48+
System.out.println(Arrays.toString(arr));
49+
50+
int[] arr1 = {1, 2, 3, 4, 5, 6, 7};
51+
rotateRight(arr1, 2);
52+
System.out.println(Arrays.toString(arr1));
53+
54+
int[] arr2 = {1, 2, 3, 4, 5, 6, 7};
55+
rotateLeft(arr2, 2);
56+
System.out.println(Arrays.toString(arr2));
57+
}
58+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.antesh.dsa.arrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* Given a fixed-length integer array arr,
9+
* duplicate each occurrence of zero, shifting the remaining elements to the
10+
* right.
11+
* <p>
12+
* Input: arr = [1,0,2,3,0,4,5,0]
13+
* Output: [1,0,0,2,3,0,0,4]
14+
* Explanation: After calling your function, the input array is modified to:
15+
* [1,0,0,2,3,0,0,4]
16+
*/
17+
public class DuplicateZeros {
18+
19+
public static void main(String[] args) {
20+
21+
System.out.println(Arrays.toString(duplicateZerosOptimizedSC(new int[]{1, 0, 2, 3, 0, 4, 5, 0})));
22+
System.out.println(Arrays.toString(duplicateZerosOptimizedTC(new int[]{1, 0, 2, 3, 0, 4, 5, 0})));
23+
System.out.println(Arrays.toString(duplicateZerosOptimizedTCOn(new int[]{1, 0, 2, 3, 0, 4, 5, 0})));
24+
25+
}
26+
27+
//TC: O(n) but SC: O(n)
28+
private static int[] duplicateZerosOptimizedTC(int[] arr) {
29+
30+
if (arr == null || arr.length == 0) {
31+
return new int[]{};
32+
}
33+
34+
List<Integer> memo = new ArrayList<>();
35+
36+
for (int num : arr) {
37+
if (num == 0) {
38+
if (memo.size() < arr.length) memo.add(num);
39+
if (memo.size() < arr.length) memo.add(num);
40+
} else {
41+
if (memo.size() < arr.length) memo.add(num);
42+
}
43+
}
44+
45+
return memo.stream().mapToInt(Integer::intValue).toArray();
46+
}
47+
48+
//TC: O(n*n) but SC: O(1)
49+
private static int[] duplicateZerosOptimizedSC(int[] arr) {
50+
51+
if (arr == null || arr.length == 0) {
52+
return new int[]{};
53+
}
54+
55+
for (int start = 0; start < arr.length; start++) {
56+
if (arr[start] == 0) {
57+
shiftToRight(arr, start);
58+
start++;
59+
}
60+
}
61+
62+
return arr;
63+
}
64+
65+
private static void shiftToRight(int[] arr, int start) {
66+
for (int end = arr.length - 1; end > start; end--) {
67+
arr[end] = arr[end - 1];
68+
}
69+
}
70+
71+
//TC: O(n*n) but SC: O(1)
72+
private static int[] duplicateZerosOptimizedTCOn(int[] arr) {
73+
if (arr == null || arr.length == 0) {
74+
return new int[]{};
75+
}
76+
77+
int startShiftingIndex = getStartShiftingIndex(arr);
78+
shiftElementsToRight(arr, startShiftingIndex);
79+
80+
return arr;
81+
}
82+
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--;
88+
} else {
89+
arr[end] = arr[startShiftingIndex];
90+
arr[end - 1] = arr[startShiftingIndex];
91+
startShiftingIndex--;
92+
end--;
93+
}
94+
}
95+
}
96+
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;
105+
}
106+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.antesh.dsa.arrays;
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+
int[] arr5 = {1, 12, 15, 26};
30+
int[] arr6 = {2, 12, 17, 30, 45};
31+
32+
System.out.println("Median is " + mergeTwoSortedArrayAndFindMedian(arr5, 0, arr6, 0));
33+
34+
35+
//If we already have sorted array and need to find median
36+
System.out.println(findMedian(new int[]{1, 2, 12, 13, 15, 24, 26, 38}));
37+
}
38+
39+
//TC: O(m+n) & SC: O(m+n)
40+
private static float mergeTwoSortedArrayAndFindMedian(int[] arr1, int m, int[] arr2, int n) {
41+
int[] sortedArrResult = new int[arr1.length + arr2.length];
42+
43+
mergeTwoSortedArray(arr1, m, arr2, n, sortedArrResult);
44+
45+
System.out.println(Arrays.toString(sortedArrResult));
46+
47+
return findMedian(sortedArrResult);
48+
}
49+
50+
private static void mergeTwoSortedArray(int[] arr1, int m, int[] arr2, int n, int[] sortedArrResult) {
51+
int start = 0;
52+
while (m < arr1.length) {
53+
if (arr1[m] < arr2[n]) {
54+
sortedArrResult[start++] = arr1[m];
55+
m++;
56+
} else {
57+
sortedArrResult[start++] = arr2[n];
58+
n++;
59+
}
60+
}
61+
62+
while (n < arr2.length) {
63+
sortedArrResult[start++] = arr2[n++];
64+
}
65+
}
66+
67+
private static float findMedian(int[] sortedArr) {
68+
if (sortedArr.length % 2 == 0) {
69+
int n1 = (sortedArr.length - 1) / 2;
70+
int n2 = sortedArr.length / 2;
71+
return (float) (sortedArr[n1] + sortedArr[n2]) / 2;
72+
}
73+
74+
int n1 = (sortedArr.length - 1) / 2;
75+
int n2 = (sortedArr.length + 1) / 2;
76+
77+
return (float) (sortedArr[n1] + sortedArr[n2]) / 2;
78+
}
79+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.antesh.dsa.arrays;
2+
3+
/* You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array
4+
[Basically you have to sort the array].
5+
Traverse array only once.
6+
Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]
7+
Output array = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
8+
9+
TC: O(n)
10+
* */
11+
12+
import java.util.Arrays;
13+
14+
public class SortZeroAndOnesArrayProblem {
15+
public static void main(String[] args) {
16+
System.out.println(Arrays.toString(sortZeroAndOnes(new int[]{0, 1, 0, 1, 0, 0, 1, 1, 1, 0})));
17+
System.out.println(Arrays.toString(sortZeroAndOnes(new int[]{1, 0, 1, 0, 0, 1, 1, 1, 1})));
18+
}
19+
20+
private static int[] sortZeroAndOnes(int[] input) {
21+
int n = input.length;;
22+
int start = 0;
23+
int end = n-1;
24+
25+
//{1, 0, 1}
26+
while (start < end) {
27+
if (input[start] == 1) {
28+
input[end] = input[end] + input[start]; //1+1 = 2 //0+1 = 1
29+
input[start] = input[end] - input[start]; //2-1 = 1 //1-0 = 0
30+
input[end] = input[end] - input[start]; //2-1 = 1 //1-0 = 1
31+
end--;
32+
} else {
33+
start++;
34+
}
35+
}
36+
return input;
37+
}
38+
}

0 commit comments

Comments
 (0)