|
@@ -1,5 +1,7 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+
|
|
4
|
+
|
3
|
5
|
public class ArrayDrills {
|
4
|
6
|
|
5
|
7
|
|
|
@@ -10,7 +12,17 @@ public class ArrayDrills {
|
10
|
12
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
13
|
*/
|
12
|
14
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
15
|
+ boolean contain = false;
|
|
16
|
+
|
|
17
|
+ for(int i = 0; i < input.length; i++){
|
|
18
|
+ if(input[0].equals(value) || input[input.length - 1].equals(value)){
|
|
19
|
+ contain = true;
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+ return contain;
|
14
|
26
|
}
|
15
|
27
|
|
16
|
28
|
/**
|
|
@@ -19,7 +31,21 @@ public class ArrayDrills {
|
19
|
31
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
32
|
*/
|
21
|
33
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
34
|
+ boolean contain = false;
|
|
35
|
+ for(int i = 0; i<input.length; i++){
|
|
36
|
+ if(input[0] == input[input.length-1]){
|
|
37
|
+ contain = true;
|
|
38
|
+
|
|
39
|
+ }
|
|
40
|
+ else{
|
|
41
|
+ contain = false;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+ return contain;
|
23
|
49
|
}
|
24
|
50
|
|
25
|
51
|
|
|
@@ -30,7 +56,18 @@ public class ArrayDrills {
|
30
|
56
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
57
|
*/
|
32
|
58
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
59
|
+ boolean contain = false;
|
|
60
|
+
|
|
61
|
+ if(input1[0] == input2[0] || input1[input1.length-1] == input2[input2.length-1]){
|
|
62
|
+ contain = true;
|
|
63
|
+ }
|
|
64
|
+ else{
|
|
65
|
+ contain = false;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+ return contain;
|
34
|
71
|
}
|
35
|
72
|
|
36
|
73
|
/**
|
|
@@ -39,7 +76,19 @@ public class ArrayDrills {
|
39
|
76
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
77
|
*/
|
41
|
78
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
79
|
+ Integer[] newArr = new Integer[input.length];
|
|
80
|
+ int temp = input[0];
|
|
81
|
+
|
|
82
|
+ for(int i = 0; i<input.length-1; i++) {
|
|
83
|
+
|
|
84
|
+ newArr[i] = input[i + 1];
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ newArr[input.length-1] = temp;
|
|
90
|
+
|
|
91
|
+ return newArr;
|
43
|
92
|
}
|
44
|
93
|
|
45
|
94
|
|
|
@@ -50,7 +99,26 @@ public class ArrayDrills {
|
50
|
99
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
100
|
*/
|
52
|
101
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
102
|
+
|
|
103
|
+ Integer[] output = new Integer[input.length];
|
|
104
|
+ int max = input[0];
|
|
105
|
+ for(int i = 0; i < input.length; i++){
|
|
106
|
+ if( input[i] > max ){
|
|
107
|
+ max = input[i];
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+ }
|
|
114
|
+ for(int j = 0; j < output.length; j++){
|
|
115
|
+ output[j] = max;
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+ return output;
|
54
|
122
|
}
|
55
|
123
|
|
56
|
124
|
|
|
@@ -61,9 +129,28 @@ public class ArrayDrills {
|
61
|
129
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
130
|
*/
|
63
|
131
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+ Integer[] mid = new Integer[2];
|
|
135
|
+ mid[0] = middle(input1);
|
|
136
|
+ mid[1] = middle(input2);
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+ return mid;
|
|
141
|
+
|
|
142
|
+
|
65
|
143
|
}
|
|
144
|
+ private int middle(Integer[] input) {
|
|
145
|
+ int value = 0;
|
|
146
|
+ if (input.length % 2 == 0) {
|
66
|
147
|
|
|
148
|
+ value = input[input.length / 2] + input[(input.length / 2) - 1];
|
|
149
|
+ } else {
|
|
150
|
+ value = input[input.length / 2];
|
|
151
|
+ }
|
|
152
|
+ return value;
|
|
153
|
+ }
|
67
|
154
|
|
68
|
155
|
/**
|
69
|
156
|
* Start with 2 int arrays, a and b, each length 2.
|
|
@@ -71,7 +158,30 @@ public class ArrayDrills {
|
71
|
158
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
159
|
*/
|
73
|
160
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
161
|
+
|
|
162
|
+ Integer[] biggest = new Integer[a.length];
|
|
163
|
+ int suma = 0;
|
|
164
|
+ int sumb = 0;
|
|
165
|
+ for(Integer i : a){
|
|
166
|
+ suma += i;
|
|
167
|
+
|
|
168
|
+ }
|
|
169
|
+ for(Integer j : b){
|
|
170
|
+
|
|
171
|
+ sumb += j;
|
|
172
|
+ }
|
|
173
|
+ if (suma >= sumb){
|
|
174
|
+
|
|
175
|
+ biggest = a;
|
|
176
|
+
|
|
177
|
+ }
|
|
178
|
+ else{
|
|
179
|
+ biggest = b;
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+ return biggest;
|
75
|
185
|
}
|
76
|
186
|
|
77
|
187
|
/**
|
|
@@ -81,6 +191,21 @@ public class ArrayDrills {
|
81
|
191
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
192
|
*/
|
83
|
193
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
194
|
+
|
|
195
|
+ Integer[] mid3 = new Integer[3];
|
|
196
|
+ int count = 0;
|
|
197
|
+ for(int i = 0; i < nums.length; i++){
|
|
198
|
+
|
|
199
|
+ if(i >= (nums.length/2)-1 && i <= (nums.length/2)+1){
|
|
200
|
+
|
|
201
|
+ mid3[count] = nums[i];
|
|
202
|
+
|
|
203
|
+ count ++;
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+ return mid3;
|
85
|
210
|
}
|
86
|
211
|
}
|