|
@@ -1,4 +1,7 @@
|
1
|
1
|
package io.zipcoder;
|
|
2
|
+import java.util.*;
|
|
3
|
+
|
|
4
|
+import static java.util.Collections.fill;
|
2
|
5
|
|
3
|
6
|
public class ArrayDrills {
|
4
|
7
|
|
|
@@ -10,7 +13,16 @@ public class ArrayDrills {
|
10
|
13
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
14
|
*/
|
12
|
15
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
16
|
+ Boolean firstLast = false;
|
|
17
|
+ for (int i = 0; i < input.length; i++) {
|
|
18
|
+ if(input[0] == value) {
|
|
19
|
+ firstLast = true;
|
|
20
|
+ } else if(input[input.length -1] == value) {
|
|
21
|
+ firstLast = true;
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ return firstLast;
|
14
|
26
|
}
|
15
|
27
|
|
16
|
28
|
/**
|
|
@@ -19,7 +31,9 @@ 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
|
+
|
|
35
|
+
|
|
36
|
+ return input.length >= 1 && input[0] == input[input.length-1];
|
23
|
37
|
}
|
24
|
38
|
|
25
|
39
|
|
|
@@ -30,7 +44,14 @@ public class ArrayDrills {
|
30
|
44
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
45
|
*/
|
32
|
46
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
47
|
+ boolean common = false;
|
|
48
|
+ if(input1[0] == input2[0]) {
|
|
49
|
+ common = true;
|
|
50
|
+ }
|
|
51
|
+ if(input1[input1.length-1] == input2[input2.length-1]){
|
|
52
|
+ common = true;
|
|
53
|
+ }
|
|
54
|
+ return common;
|
34
|
55
|
}
|
35
|
56
|
|
36
|
57
|
/**
|
|
@@ -39,7 +60,16 @@ public class ArrayDrills {
|
39
|
60
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
61
|
*/
|
41
|
62
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
63
|
+
|
|
64
|
+ List<Integer> integerList = Arrays.asList(input);
|
|
65
|
+
|
|
66
|
+ Collections.rotate(integerList, -1);
|
|
67
|
+
|
|
68
|
+ Integer[] newArray = integerList.toArray(new Integer[integerList.size()]);
|
|
69
|
+
|
|
70
|
+ return newArray;
|
|
71
|
+
|
|
72
|
+
|
43
|
73
|
}
|
44
|
74
|
|
45
|
75
|
|
|
@@ -50,7 +80,15 @@ public class ArrayDrills {
|
50
|
80
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
81
|
*/
|
52
|
82
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
83
|
+
|
|
84
|
+ List<Integer> integerList = Arrays.asList(input);
|
|
85
|
+
|
|
86
|
+ fill(integerList, Collections.max(integerList));
|
|
87
|
+
|
|
88
|
+ Integer[] newArray = integerList.toArray(new Integer[integerList.size()]);
|
|
89
|
+
|
|
90
|
+ return newArray;
|
|
91
|
+
|
54
|
92
|
}
|
55
|
93
|
|
56
|
94
|
|
|
@@ -61,7 +99,24 @@ public class ArrayDrills {
|
61
|
99
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
100
|
*/
|
63
|
101
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
102
|
+ Integer[] newArray = new Integer[2];
|
|
103
|
+ if(input1.length % 2 == 1) {
|
|
104
|
+ Integer middle = input1.length / 2;
|
|
105
|
+ newArray[0] = input1[middle];
|
|
106
|
+ }
|
|
107
|
+ if(input1.length % 2 == 0) {
|
|
108
|
+ Integer add = input1.length / 2;
|
|
109
|
+ newArray[0] = input1[add-1] + input1[add];
|
|
110
|
+ }
|
|
111
|
+ if(input2.length % 2 == 1) {
|
|
112
|
+ Integer mid = input2.length / 2;
|
|
113
|
+ newArray[1] = input2[mid];
|
|
114
|
+ }
|
|
115
|
+ if(input2.length % 2 == 0) {
|
|
116
|
+ Integer add = input2.length / 2;
|
|
117
|
+ newArray[1] = input2[add-1] + input2[add];
|
|
118
|
+ }
|
|
119
|
+ return newArray;
|
65
|
120
|
}
|
66
|
121
|
|
67
|
122
|
|
|
@@ -71,7 +126,13 @@ public class ArrayDrills {
|
71
|
126
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
127
|
*/
|
73
|
128
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
129
|
+
|
|
130
|
+ if(a[0] + a[1] > b[0] + b[1]) {
|
|
131
|
+ return a;
|
|
132
|
+ } else {
|
|
133
|
+ return b;
|
|
134
|
+ }
|
|
135
|
+
|
75
|
136
|
}
|
76
|
137
|
|
77
|
138
|
/**
|
|
@@ -81,6 +142,12 @@ public class ArrayDrills {
|
81
|
142
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
143
|
*/
|
83
|
144
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
145
|
+ Integer[] newArray = new Integer[3];
|
|
146
|
+ int halfway = nums.length/2;
|
|
147
|
+ newArray[0] = nums[halfway -1];
|
|
148
|
+ newArray[1] = nums[halfway];
|
|
149
|
+ newArray[2] = nums[halfway +1];
|
|
150
|
+
|
|
151
|
+ return newArray;
|
85
|
152
|
}
|
86
|
153
|
}
|