|
@@ -1,5 +1,10 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.List;
|
|
4
|
+
|
|
5
|
+import static java.util.Arrays.*;
|
|
6
|
+import static java.util.Collections.*;
|
|
7
|
+
|
3
|
8
|
public class ArrayDrills {
|
4
|
9
|
|
5
|
10
|
|
|
@@ -10,7 +15,20 @@ public class ArrayDrills {
|
10
|
15
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
16
|
*/
|
12
|
17
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
18
|
+ boolean answer = false;
|
|
19
|
+
|
|
20
|
+ for(int i = 0; i < input.length; i++) {
|
|
21
|
+ if (input[0] == value) {
|
|
22
|
+ answer = true;
|
|
23
|
+
|
|
24
|
+ } else if (input[input.length - 1] == value) {
|
|
25
|
+ answer = true;
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+ return answer;
|
14
|
32
|
}
|
15
|
33
|
|
16
|
34
|
/**
|
|
@@ -19,7 +37,9 @@ public class ArrayDrills {
|
19
|
37
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
38
|
*/
|
21
|
39
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
40
|
+ return input.length > 0 && input[0] == input[input.length -1] ? true : false;
|
|
41
|
+
|
|
42
|
+
|
23
|
43
|
}
|
24
|
44
|
|
25
|
45
|
|
|
@@ -30,7 +50,22 @@ public class ArrayDrills {
|
30
|
50
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
51
|
*/
|
32
|
52
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
53
|
+ boolean common = false;
|
|
54
|
+
|
|
55
|
+ if (input1[0] == input2[0]) {
|
|
56
|
+ common = true;
|
|
57
|
+
|
|
58
|
+ }
|
|
59
|
+ if (input1[input1.length-1] == input2[input2.length-1]) {
|
|
60
|
+
|
|
61
|
+ common = true;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ //return input1[input1.length-1] == input2[input2.length-1] || input1[0] == input2[0];
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+ return common;
|
|
68
|
+
|
34
|
69
|
}
|
35
|
70
|
|
36
|
71
|
/**
|
|
@@ -39,7 +74,15 @@ public class ArrayDrills {
|
39
|
74
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
75
|
*/
|
41
|
76
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
77
|
+
|
|
78
|
+ List<Integer> arrayList = asList(input);
|
|
79
|
+
|
|
80
|
+ rotate(arrayList, -1);
|
|
81
|
+
|
|
82
|
+ Integer[] arr = arrayList.toArray(new Integer[arrayList.size()]);
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+ return arr;
|
43
|
86
|
}
|
44
|
87
|
|
45
|
88
|
|
|
@@ -50,7 +93,23 @@ public class ArrayDrills {
|
50
|
93
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
94
|
*/
|
52
|
95
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
96
|
+ List<Integer> arrayList = asList(input);
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+ fill(arrayList, max(arrayList));
|
|
100
|
+
|
|
101
|
+ Integer[] arr = arrayList.toArray(new Integer[arrayList.size()]);
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+ return arr;
|
54
|
113
|
}
|
55
|
114
|
|
56
|
115
|
|
|
@@ -60,8 +119,27 @@ public class ArrayDrills {
|
60
|
119
|
* example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
|
61
|
120
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
121
|
*/
|
63
|
|
- public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
122
|
+ public Integer[] middleWay(Integer[] input1, Integer[] input2) {
|
|
123
|
+
|
|
124
|
+ Integer[] array = new Integer[2];
|
|
125
|
+
|
|
126
|
+ if (input1.length % 2 != 0) {
|
|
127
|
+
|
|
128
|
+ array[0] = input1[input1.length / 2];
|
|
129
|
+ } else if (input1.length % 2 == 0) {
|
|
130
|
+ array[0] = input1[input1.length / 2] + input1[input1.length / 2 - 1];
|
|
131
|
+
|
|
132
|
+ }
|
|
133
|
+ if (input2.length % 2 != 0) {
|
|
134
|
+
|
|
135
|
+ array[1] = input2[input2.length / 2];
|
|
136
|
+ } else if (input2.length % 2 == 0) {
|
|
137
|
+ array[1] = input2[input2.length / 2] + input2[input2.length / 2 - 1];
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+ }
|
|
142
|
+ return array;
|
65
|
143
|
}
|
66
|
144
|
|
67
|
145
|
|
|
@@ -71,7 +149,15 @@ public class ArrayDrills {
|
71
|
149
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
150
|
*/
|
73
|
151
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
152
|
+ if (a[0] + a[1] > b[0] + b[1]){
|
|
153
|
+ return a;
|
|
154
|
+ }
|
|
155
|
+ return b;
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
75
|
161
|
}
|
76
|
162
|
|
77
|
163
|
/**
|
|
@@ -81,6 +167,19 @@ public class ArrayDrills {
|
81
|
167
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
168
|
*/
|
83
|
169
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
170
|
+ Integer[] halfArray = new Integer[3];
|
|
171
|
+ int half = nums.length/2;
|
|
172
|
+ halfArray[0] = nums[half-1];
|
|
173
|
+ halfArray[1] = nums[half];
|
|
174
|
+ halfArray[2] = nums[half+1];
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+ return halfArray;
|
85
|
184
|
}
|
86
|
185
|
}
|